Solved

VBS script worked on XP but not on W7

Posted on 2014-01-16
17
342 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

827 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