Solved

VBS script worked on XP but not on W7

Posted on 2014-01-16
17
347 Views
Last Modified: 2014-01-22
Hi, an EE member created me a VBS script to switch default gateway on client machines. This used to work fine on XP desktop, but since moving to Windows 7, it no longer works. The network interface is still called "Local Area Connection" Anyone know why this won't work on W7?

Renamed attachment from .hta to .txt to allow upload to EE.

Thanks.
Switch-Gateway.txt
0
Comment
Question by:scribla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
17 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 39786306
From a command prompt on a Win7 box type
netsh interface /?

Note that there is ipv4 & ipv6 but not ip.  I haven't tried it but I suspect you simply need to update your script so that it reads
objShell.Run "netsh interface ipv4 set address name=""Local Area Connection"" static gateway=" & strGateway & " gwmetric=" & intGatewayMetric, 0, True

OM Gang
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39787036
Hi, try this out.

Note that I have combined the three gateway subs into one, just passing the gateway value from the button that was clicked.  The test I have made for XP or Win7 is to check for "C:\Windows\SYSWOW64", which of course assumes that your Win7 machines are 64 bit.  If they are not, probably safe enough to check for "C:\Users" or "C:\ProgramData" instead.

Regards,

Rob.

<head>

	<title>
	Set Default Gateway
	</title>
	
	<HTA:APPLICATION 
	APPLICATION ID="Set Gateway" 
	APPLICATIONNAME="Set Gateway" 
	BORDERSTYLE = "complex"
	BORDER = "dialog" 
	CAPTION = "yes" 
	RESIZE = "no" 
	SHOWINTASKBAR = "yes" 
	SINGLEINSTANCE = "yes" 
	SYSMENU = "no" 
	WINDOWSTATE = "normal" 
	SCROLL = "no" 
	SCROLLFLAT = "yes" 
	VERSION = "1.0" 
	INNERBORDER = "no" 
	SELECTION = "no" 
	MAXIMIZEBUTTON = "no" 
	MINIMIZEBUTTON = "yes" 
	NAVIGABLE = "yes" 
	CONTEXTMENU = "yes" 
	BORDERSTYLE = "normal"
	sysMenu="yes"
	icon = "\\dartford\home\dcollings\roaming\desktop\elite-desktop-pack-01\ICO\My Network.ico"> 
	</HTA>
	
</head>
<font size="2" face="Century Gothic, Tahoma, Arial, Helvetica">
<body bgcolor="#000000">
<script language="VBScript">

    Sub Window_Onload
		window.offscreenBuffering = True
		window.resizeTo 370,330
	End Sub
	
    Sub Gateway(strGateway)
		Dim strOption, objFSO
		Dim objShell, objButton
		On Error Resume Next
		
		Set objShell = CreateObject("Wscript.Shell")
		Set objFSO = CreateObject("WScript.Network")
		intGatewayMetric = 1

		Results.InnerHTML = "<font color=red>Setting gateway to " & strGateway & "</font>"

		If objFSO.FolderExists("C:\Windows\SYSWOW64") = False Then
			objShell.Run "netsh interface ip set address name=""Local Area Connection"" static gateway=" & strGateway & " gwmetric=" & intGatewayMetric, 0, True
		Else
			objShell.Run "netsh interface ipv4 set address name=""Local Area Connection"" static gateway=" & strGateway & " gwmetric=" & intGatewayMetric, 0, True
		End If
		Set objShell = Nothing

		Results.InnerHTML = "<font color=red>Done!</font>"

		self.close()
	End Sub

	Sub ExitHTA
	    self.close()
	End Sub
	
</script>

<body>
	<center>
		<input type="button" value="Star 10.32.0.1" name="run_button" onClick="Gateway('10.32.0.1')"><p>
		<input type="button" value="Sky 10.32.4.100" name="run_button" onClick="Gateway('10.32.4.100')"><p>
		<input type="button" value="Gamma 10.32.0.2" name="run_button" onClick="Gateway('10.32.0.2')"><p>
<input type="button" value="Cancel" name="quit_button"  onClick="ExitHTA"><p>

		<font size=4>
			<span id = "Results"></span>
		</font>
	</center>
</body>

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39787040
Oh, and of course I have incorporated OMGang's fix.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:scribla
ID: 39788084
The above script works fine with XP, but doesn't change the gateway on W7 64. Also to ease troubleshooting, would it be possible to get it to print the current gateway?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39788194
I think the easiest way would be for you to run the netsh command on a Win7 64 bit machine using ipv4 and see whether there's an error. If you can get a manual command working, we can easily put that in the script.

One thing though, on Win7 do you have UAC enabled?  You may need to run a command prompt "as Administrator" and then run:
mshta.exe C:\Scripts\YourHTA.hta

So that it has full rights.

Rob.
0
 
LVL 2

Author Comment

by:scribla
ID: 39788329
According to this solution, using netsh is going to cause problems, as well as UAC and running  as admin.
It has been suggested to use:
route change -p 0.0.0.0 mask 0.0.0.0 x.x.x.x

However I can't even get it work on the command line.
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Windows/Q_27915895.html
0
 
LVL 2

Author Comment

by:scribla
ID: 39788827
Works using "route change 0.0.0.0 MASK 0.0.0.0 10.32.4.100 metric 1" problem is the IF (interface) is different from machine to machine. If you could just pop that into the script and make it display the current gateway name or IP, the points are yours. Thanks for your help either way.
0
 
LVL 2

Author Comment

by:scribla
ID: 39788829
Sorry just to make this more clear should some one search for this, the other problem with using netsh to change the gateway, it seems to knock out a static IP to DHCP.
0
 
LVL 2

Author Comment

by:scribla
ID: 39788947
I'm really struggling to get this to work in the script.
placing "route change 0.0.0.0 mask 0.0.0.0 10.32.4.100 metric 1" in a batch file works fine. I must have done something with the script wrong:

<head>

	<title>
	Set Default Gateway
	</title>
	
	<HTA:APPLICATION 
	APPLICATION ID="Set Gateway" 
	APPLICATIONNAME="Set Gateway" 
	BORDERSTYLE = "complex"
	BORDER = "dialog" 
	CAPTION = "yes" 
	RESIZE = "no" 
	SHOWINTASKBAR = "yes" 
	SINGLEINSTANCE = "yes" 
	SYSMENU = "no" 
	WINDOWSTATE = "normal" 
	SCROLL = "no" 
	SCROLLFLAT = "yes" 
	VERSION = "1.0" 
	INNERBORDER = "no" 
	SELECTION = "no" 
	MAXIMIZEBUTTON = "no" 
	MINIMIZEBUTTON = "yes" 
	NAVIGABLE = "yes" 
	CONTEXTMENU = "yes" 
	BORDERSTYLE = "normal"
	sysMenu="yes"
	icon = "\\dartford\home\dcollings\roaming\desktop\elite-desktop-pack-01\ICO\My Network.ico"> 
	</HTA>
	
</head>
<font size="2" face="Century Gothic, Tahoma, Arial, Helvetica">
<body bgcolor="#000000">
<script language="VBScript">

    Sub Window_Onload
		window.offscreenBuffering = True
		window.resizeTo 370,330
	End Sub
	
    Sub Gateway(strGateway)
		Dim strOption, objFSO
		Dim objShell, objButton
		On Error Resume Next
		
		Set objShell = CreateObject("Wscript.Shell")
		Set objFSO = CreateObject("WScript.Network")
		intGatewayMetric = 1

		Results.InnerHTML = "<font color=red>Setting gateway to " & strGateway & "</font>"

		If objFSO.FolderExists("C:\Windows\SYSWOW64") = False Then
			objShell.Run "netsh interface ip set address name=""Local Area Connection"" static gateway=" & strGateway & " gwmetric=" & intGatewayMetric, 0, True
		Else
			objShell.Run "route change 0.0.0.0 MASK 0.0.0.0 " & strGateway & " METRIC 1", 0, True
		End If
		Set objShell = Nothing

		Results.InnerHTML = "<font color=red>Done!</font>"


	End Sub

	Sub ExitHTA
	    self.close()
	End Sub
	
</script>

<body>
	<center>
		<input type="button" value="Star 10.32.0.1" name="run_button" onClick="Gateway('10.32.0.1')"><p>
		<input type="button" value="Sky 10.32.4.100" name="run_button" onClick="Gateway('10.32.4.100')"><p>
		<input type="button" value="Gamma 10.32.0.2" name="run_button" onClick="Gateway('10.32.0.2')"><p>
<input type="button" value="Cancel" name="quit_button"  onClick="ExitHTA"><p>

		<font size=4>
			<span id = "Results"></span>
		</font>
	</center>
</body> 

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39790580
Try using
objShell.Run "cmd /c route change 0.0.0.0 mask 0.0.0.0 " & strGateway & " metric 1", 0, True

And again, you may need to run the HTA "as Administrator" as I described above.

Rob.
0
 
LVL 2

Author Comment

by:scribla
ID: 39793646
That didn't work either.
But then I removed the IF/ELSE sections, and it works fine (on both XP and W7) due to using Route over Netsh.  So it seems all along it was the if/else causing the problem. I can confirm this was on Win7/64 and the folder does indeed exist. As I say no longer a problem due to using the route command rather than netsh.
Is it simple to display the current gateway above the buttons when the hta launches?

For anyone searching, the code below works.

<head>

	<title>
	Set Default Gateway
	</title>
	
	<HTA:APPLICATION 
	APPLICATION ID="Set Gateway" 
	APPLICATIONNAME="Set Gateway" 
	BORDERSTYLE = "complex"
	BORDER = "dialog" 
	CAPTION = "yes" 
	RESIZE = "no" 
	SHOWINTASKBAR = "yes" 
	SINGLEINSTANCE = "yes" 
	SYSMENU = "no" 
	WINDOWSTATE = "normal" 
	SCROLL = "no" 
	SCROLLFLAT = "yes" 
	VERSION = "1.0" 
	INNERBORDER = "no" 
	SELECTION = "no" 
	MAXIMIZEBUTTON = "no" 
	MINIMIZEBUTTON = "yes" 
	NAVIGABLE = "yes" 
	CONTEXTMENU = "yes" 
	BORDERSTYLE = "normal"
	sysMenu="yes"
	icon = "\\dartford\home\dcollings\roaming\desktop\elite-desktop-pack-01\ICO\My Network.ico"> 
	</HTA>
	
</head>
<font size="2" face="Century Gothic, Tahoma, Arial, Helvetica">
<body bgcolor="#000000">
<script language="VBScript">

    Sub Window_Onload
		window.offscreenBuffering = True
		window.resizeTo 370,330
	End Sub
	
    Sub Gateway(strGateway)
		Dim strOption, objFSO
		Dim objShell, objButton
		On Error Resume Next
		
		Set objShell = CreateObject("Wscript.Shell")
		Set objFSO = CreateObject("WScript.Network")
		intGatewayMetric = 1

		Results.InnerHTML = "<font color=red>Setting gateway to " & strGateway & "</font>"


			objShell.Run "cmd /c route change 0.0.0.0 mask 0.0.0.0 " & strGateway & " metric 1", 0, True



		Set objShell = Nothing

		Results.InnerHTML = "<font color=red>Done!</font>"


	End Sub

	Sub ExitHTA
	    self.close()
	End Sub
	
</script>

<body>
	<center>
		<input type="button" value="Star 10.32.0.1" name="run_button" onClick="Gateway('10.32.0.1')"><p>
		<input type="button" value="Sky 10.32.4.100" name="run_button" onClick="Gateway('10.32.4.100')"><p>
		<input type="button" value="Gamma 10.32.0.2" name="run_button" onClick="Gateway('10.32.0.2')"><p>
<input type="button" value="Cancel" name="quit_button"  onClick="ExitHTA"><p>

		<font size=4>
			<span id = "Results"></span>
		</font>
	</center>
</body> 

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39793664
Yeah, easy enough if you add a function to get the gateway...I'll add that in the morning.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39795515
Try this.

Regards,

Rob.

<head>

	<title>
	Set Default Gateway
	</title>
	
	<HTA:APPLICATION 
	APPLICATION ID="Set Gateway" 
	APPLICATIONNAME="Set Gateway" 
	BORDERSTYLE = "complex"
	BORDER = "dialog" 
	CAPTION = "yes" 
	RESIZE = "no" 
	SHOWINTASKBAR = "yes" 
	SINGLEINSTANCE = "yes" 
	SYSMENU = "no" 
	WINDOWSTATE = "normal" 
	SCROLL = "no" 
	SCROLLFLAT = "yes" 
	VERSION = "1.0" 
	INNERBORDER = "no" 
	SELECTION = "no" 
	MAXIMIZEBUTTON = "no" 
	MINIMIZEBUTTON = "yes" 
	NAVIGABLE = "yes" 
	CONTEXTMENU = "yes" 
	BORDERSTYLE = "normal"
	sysMenu="yes"
	icon = "\\dartford\home\dcollings\roaming\desktop\elite-desktop-pack-01\ICO\My Network.ico"> 
	</HTA>
	
</head>
<font size="2" face="Century Gothic, Tahoma, Arial, Helvetica">
<body bgcolor="#000000">
<script language="VBScript">

    Sub Window_Onload
		window.offscreenBuffering = True
		window.resizeTo 370,330
		
	Results.InnerHTML = "<font color=red>Current gateway:" & GetGateway & "</font>"
		
	End Sub
	
    Sub Gateway(strGateway)
		Dim strOption, objFSO
		Dim objShell, objButton
		On Error Resume Next
		
		Set objShell = CreateObject("Wscript.Shell")
		Set objFSO = CreateObject("WScript.Network")
		intGatewayMetric = 1

		Results.InnerHTML = "<font color=red>Setting gateway to " & strGateway & "</font>"


			objShell.Run "cmd /c route change 0.0.0.0 mask 0.0.0.0 " & strGateway & " metric 1", 0, True



		Set objShell = Nothing

		Results.InnerHTML = "<font color=red>Done!</font>"


	End Sub

	Sub ExitHTA
	    self.close()
	End Sub
	
	Function GetGateway
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
	
		Set colComputerIP = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
		
		strGateway = "<BR>"
		For Each IPConfig In colComputerIP
			If Not IsNull(IPConfig.DefaultIPGateway) Then
				strGateway = strGateway & Join(IPConfig.DefaultIPGateway) & "<BR>"
			End If
		Next
		GetGateway = strGateway
	End Function
</script>

<body>
	<center>
		<input type="button" value="Star 10.32.0.1" name="run_button" onClick="Gateway('10.32.0.1')"><p>
		<input type="button" value="Sky 10.32.4.100" name="run_button" onClick="Gateway('10.32.4.100')"><p>
		<input type="button" value="Gamma 10.32.0.2" name="run_button" onClick="Gateway('10.32.0.2')"><p>
<input type="button" value="Cancel" name="quit_button"  onClick="ExitHTA"><p>

		<font size=4>
			<span id = "Results"></span>
		</font>
	</center>
</body>

Open in new window

0
 
LVL 2

Author Comment

by:scribla
ID: 39796596
Line 79
Char 4
Error: Invalid use of Null: 'Join'
Code: 0

Let me know if it is not any easy fix, I'll award points anyway, this was the icing on the cake as it where. Thanks.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 39798398
OK, try the code again.  I have changed
				strGateway = strGateway & Join(IPConfig.DefaultIPGateway) & "<BR>"

Open in new window


to this:
			If Not IsNull(IPConfig.DefaultIPGateway) Then
				strGateway = strGateway & Join(IPConfig.DefaultIPGateway) & "<BR>"
			End If

Open in new window


Regards,

Rob.
0
 
LVL 2

Author Closing Comment

by:scribla
ID: 39800463
Works great Rob! Many thanks for your continued efforts and going the extra mile. Cheers!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39801365
No problem. Thanks for the grade.

Rob.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question