Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change gateway and subnet with script?

Status
Not open for further replies.

WilliamUT

IS-IT--Management
Oct 8, 2002
182
US
Is there a script to change the subnet and gateway on a computer? Thanks
 
The script below has been taken from microsoft's script center, here is the link
This is to set a static ip address but does the subnet and gateway

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array("192.168.1.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next




Regards
Steve Friday
 
thanks! i actually wrote a script using the netsh command line to accomplish what i needed. Is there a way though to read the subnet and gateway with a script?
 
again taken from microsoft script centre, you will have to change the script a bit as it will return a lot more info than you require.


' Returning IP Configuration Data
' WMI script that returns configuration data similar to that returned by IpConfig.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" &amp & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

n = 1
WScript.Echo

For Each objAdapter in colAdapters

WScript.Echo "Network Adapter " & n
WScript.Echo "================="
WScript.Echo " Description: " & objAdapter.Description
WScript.Echo " Physical (MAC) address: " & objAdapter.MACAddress
WScript.Echo " Host name: " & objAdapter.DNSHostName

If Not IsNull(objAdapter.IPAddress) Then
For i = 0 To UBound(objAdapter.IPAddress)
WScript.Echo " IP address: " & objAdapter.IPAddress(i)
Next
End If

If Not IsNull(objAdapter.IPSubnet) Then
For i = 0 To UBound(objAdapter.IPSubnet)
WScript.Echo " Subnet: " & objAdapter.IPSubnet(i)
Next
End If

If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = 0 To UBound(objAdapter.DefaultIPGateway)
WScript.Echo " Default gateway: " & objAdapter.DefaultIPGateway(i)
Next
End If

WScript.Echo
WScript.Echo " DNS"
WScript.Echo " ---"
WScript.Echo " DNS servers in search order:"

If Not IsNull(objAdapter.DNSServerSearchOrder) Then
For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
WScript.Echo " " & objAdapter.DNSServerSearchOrder(i)
Next
End If

WScript.Echo " DNS domain: " & objAdapter.DNSDomain

If Not IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)
WScript.Echo " DNS suffix search list: " & objAdapter.DNSDomainSuffixSearchOrder(i)
Next
End If

WScript.Echo
WScript.Echo " DHCP"
WScript.Echo " ----"
WScript.Echo " DHCP enabled: " & objAdapter.DHCPEnabled
WScript.Echo " DHCP server: " & objAdapter.DHCPServer

If Not IsNull(objAdapter.DHCPLeaseObtained) Then
utcLeaseObtained = objAdapter.DHCPLeaseObtained
strLeaseObtained = WMIDateStringToDate(utcLeaseObtained)
Else
strLeaseObtained = ""
End If
WScript.Echo " DHCP lease obtained: " & strLeaseObtained

If Not IsNull(objAdapter.DHCPLeaseExpires) Then
utcLeaseExpires = objAdapter.DHCPLeaseExpires
strLeaseExpires = WMIDateStringToDate(utcLeaseExpires)
Else
strLeaseExpires = ""
End If
WScript.Echo " DHCP lease expires: " & strLeaseExpires

WScript.Echo
WScript.Echo " WINS"
WScript.Echo " ----"
WScript.Echo " Primary WINS server: " & objAdapter.WINSPrimaryServer
WScript.Echo " Secondary WINS server: " & objAdapter.WINSSecondaryServer
WScript.Echo

n = n + 1

Next

Function WMIDateStringToDate(utcDate)
WMIDateStringToDate = CDate(Mid(utcDate, 5, 2) & "/" & _
Mid(utcDate, 7, 2) & "/" & _
Left(utcDate, 4) & " " & _
Mid (utcDate, 9, 2) & ":" & _
Mid(utcDate, 11, 2) & ":" & _
Mid(utcDate, 13, 2))
End Function








Regards
Steve Friday
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top