Solved

VBS script worked on XP but not on W7

Posted on 2014-01-16
17
335 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

20 Experts available now in Live!

Get 1:1 Help Now