Link to home
Start Free TrialLog in
Avatar of scribla
scriblaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VBS script worked on XP but not on W7

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
Avatar of omgang
omgang
Flag of United States of America image

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
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

Oh, and of course I have incorporated OMGang's fix.
Avatar of scribla

ASKER

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?
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.
Avatar of scribla

ASKER

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.
https://www.experts-exchange.com/questions/27915895/NETSH-Change-Default-Gateway-Only.html
Avatar of scribla

ASKER

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.
Avatar of scribla

ASKER

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.
Avatar of scribla

ASKER

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

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.
Avatar of scribla

ASKER

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

Yeah, easy enough if you add a function to get the gateway...I'll add that in the morning.
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

Avatar of scribla

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scribla

ASKER

Works great Rob! Many thanks for your continued efforts and going the extra mile. Cheers!
No problem. Thanks for the grade.

Rob.