We are making some changes in our environment and I want to script the changing of our users' default gateway, subnet mask and their DNS Server list.
I have this script, which works on some machines. On others, it "appears" to change everything. If you manually go look, the updated settings are there. But if you run ipconfig /all, the gateway shows as blank until either a reboot or a disable/enable of the connection.
Two questions...
1. Is my script doing something that could cause this issue?
2. If not, is there a way to script the disable/enable of a network connection? I can't find any WMI methods to do so.
Thanks.
Thanks,
Andrew
Hard work often pays off over time, but procrastination pays off right now!
I have this script, which works on some machines. On others, it "appears" to change everything. If you manually go look, the updated settings are there. But if you run ipconfig /all, the gateway shows as blank until either a reboot or a disable/enable of the connection.
Two questions...
1. Is my script doing something that could cause this issue?
2. If not, is there a way to script the disable/enable of a network connection? I can't find any WMI methods to do so.
Thanks.
Code:
strComputer = "."
Set objShell = Wscript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
forceUseCScript
Sub forceUseCScript()
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
objShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34),1,False
WScript.Quit 0
End If
End Sub
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.154.62")
arrDNSServers = Array("192.168.153.12", "192.168.153.18", "192.168.48.102")
strGatewayMetric = Array(1)
For Each netCard in colNetCards
If Not IsNull(netCard.IPAddress) Then
For i=LBound(netCard.IPAddress) to UBound(netCard.IPAddress)
If Left (netCard.IPAddress(i), 8) = "192.168." Then
wscript.echo "Changing settings for card with IP Address " & netCard.IPAddress(i) & "..."
arrIPAddress = Array(netCard.IPAddress(i))
netCard.SetDNSServerSearchOrder arrDNSServers
netCard.SetGateways strGateway, strGatewayMetric
netCard.EnableStatic arrIPAddress, strSubnetMask
End If
Next
End If
Next
WScript.Sleep(5000)
wscript.echo "Complete!"
WScript.Sleep(3000)
Thanks,
Andrew
Hard work often pays off over time, but procrastination pays off right now!