Solved

VBS script worked on XP but not on W7

Posted on 2014-01-16
17
331 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
Comment Utility
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
Comment Utility
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
Comment Utility
Oh, and of course I have incorporated OMGang's fix.
0
 
LVL 2

Author Comment

by:scribla
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:scribla
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Works great Rob! Many thanks for your continued efforts and going the extra mile. Cheers!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
No problem. Thanks for the grade.

Rob.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now