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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chaging Gateway/Mask/DNS Servers 2

Status
Not open for further replies.

acl03

MIS
Jun 13, 2005
1,077
US
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.

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

[smarty] Hard work often pays off over time, but procrastination pays off right now!
 
I don't think your script is causing it. I adapted some code for this that checks the return codes for the EnableStatic and SetGateways methods for just this reason. Unfortunately I don't know what causes it either.
Code:
sub SetStatic()
	strIPAddress = Array("192.168.0.11")
	strSubnetMask= Array("255.255.255.0")
	strGateway = Array("192.168.0.1")
	strGatewayMetric = Array(1)

	for each objAdapter in colAdapters
		errIP = objAdapter.EnableStatic(strIPAddress,strSubnetMask)
		errGateway= objAdapter.SetGateways(strGateway, strGatewayMetric)
		
		if errIP = 0 and errGateway = 0 then
			WScript.Echo "IP address configured."
		elseif errIP = 1 or errGateway = 1 then
			WScript.Echo "You must reboot for the changes to take effect."
		else
			WScript.Echo "Unable to configure IP address. IP: " & errIP & " and Gateway:" & errGateway
		end if
	next
end sub

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
I was planning on adding in the same type of error checking after reading up on the return codes.

What return code do you get when this occurs? (1 or something else?)

Thanks, Jeff.

Thanks,
Andrew

[smarty] Hard work often pays off over time, but procrastination pays off right now!
 
devcon can be used to disable/enable a network connection.
 
devcon seems like an interesting command, but is there a way to run it on a random machine and know which hardware is the network card?


Thanks,
Andrew

[smarty] Hard work often pays off over time, but procrastination pays off right now!
 
My version is just used on a couple of notebooks that I need to regularly switch between a static netw0rk and a dhcp one. I've never had it tell me I needed to reboot to apply the settings.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
I'm in a similar position to Jeff (with a Work and Home network) and use the following in BAT files to swap the config over :

SET DNS TO IP
netsh int ip set dns "Wireless Network Connection 3" static 1.2.3.4 primary
netsh int ip add dns "Wireless Network Connection 3" 1.2.3.5

SET DNS TO DHCP
netsh int ip set dns "Wireless Network Connection 3" dhcp

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
I was just looking into using a shell command from vbscript to call netsh, and it works well. Problem is, it does it based on connection name and not IP.

I only want to do this on connections that already have a 192.168.x.x address statically defined, hence this code in my script:

Code:
 If Left (netCard.IPAddress(i), 8) = "192.168." Then

So if I know the IP address of a connection, can I use VBS to pull out the connection name (eg. Local Area Connection), then supply that connection name to my netsh command? Some laptops here have their wireless connection (which I want to leave alone) called "Local Area Connection" and the wired connection called "Local Area Connection 2".

This means that I can't simply use netsh on "Local Area Connection". I already have the IP address in my loop, any way to grab the name?

Thanks,
Andrew

[smarty] Hard work often pays off over time, but procrastination pays off right now!
 
acl03, if you can identify the nicconfig you are interested in from the IPaddress then the thing which ties this to the Win32_NetworkAdapter class is the .MACAddress property.
i always talk in terms of MACAddress and just flick between the Win32_NetworkAdapter and Configuration classes using the MACAddress as the 'key'. (i am sure the AssociatorsOf WMI thing will do the same thing though)
so, the MACAddress way...

For Each netCard...
If Left (netCard.IPAddress(i), 8) = "192.168." Then
strTargetMAC = netCard.MACAddress
'perhaps you can have more than one? a list?
'Exit For?
End If
Next

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter")
For Each oInstance In colItems
If objItem.MACAddress = strTargetMAC Then
strNetConnectionID = objItem.NetConnectionID
'Exit For???
End If
Next
Msgbox strNetConnectionID
'netsh against the strNetConnectionID....although the WMI classes do allow the netsh functionality
 
MrMovie - You know, that's exactly what I ended up doing...

Code:
changeNetworkSettings

Sub changeNetworkSettings


	Set objSysInfo  = CreateObject("ADSystemInfo")
	ouString        = Right(UCase ("LDAP://" & objSysInfo.ComputerName),47)
	computerOU      = "OU=COMPUTERS,OU=CENTRAL,OU=my,DC=company,DC=com"
	
	
	'---Check to see if computer is in/under computer OU
	If ouString <> computerOU Then
		MsgBox "Computer in wrong OU, quitting script."
		Exit Sub
	End If

strComputer = "."
Set objShell = Wscript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colConfiguration = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
Set colNetworkAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapter")

arrDNSServers = Array("192.168.153.12", "192.168.153.18", "192.168.48.102", "192.168.96.15","192.168.96.1")

For Each netConfiguration in colConfiguration
	If Not IsNull(netConfiguration.IPAddress) Then 
		octet3 = Mid(netConfiguration.IPAddress(0), 9, 3)
		If octet3 >= 153 And octet3 <= 155 Then
			'Set DNS server order
			netConfiguration.SetDNSServerSearchOrder arrDNSServers
			curIP = netConfiguration.IPAddress(i)
			configMacAddress = netConfiguration.MACAddress
			
			For Each netAdapter in colNetworkAdapters
				adapterMacAddress = netAdapter.MACAddress
				If   adapterMacAddress = configMacAddress And Not IsNull(netAdapter.NetConnectionID) Then
						result = objShell.run ("netsh interface ip set address """ & netAdapter.NetConnectionID & """ static " & curIP & " 255.255.255.0 192.168.154.62 1", 0, 1)
				End If
			Next
		End If
	End If
Next

End Sub

Thanks,
Andrew

[smarty] Hard work often pays off over time, but procrastination pays off right now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top