Articles

Solved VBscript to change ip and DNS

April 21, 2010 at 01:20:23
Specs: Windows 2003 server, 2G

My script don't seem to change the DNS1 and DNS 2, can some-one help me please.
Thanks

Dim strIPAddress
Dim strSubnetMask
Dim strGateway
Dim intGatewayMetric
Dim strDns1
Dim strDns2

strIPAddress = "192.168.0.45"
strSubnetMask = "255.255.252.0"
strGateway = "192.168.0.1"
intGatewayMetric = 1
strDns1 = "192.168.0.103"
strDns2 = "192.168.0.200"

Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
'objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection" & strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection" & strDns2, 0, True
Set objShell = Nothing
WScript.Quit


See More: VBscript to change ip and DNS

Report •


#1
April 21, 2010 at 08:04:43

replace

objShell.Run "netsh interface ip add dns name=""Local Area Connection" & strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection" & strDns2, 0, True

with

objShell.Run "netsh interface ip set dns name=""Local Area Connection"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection"" addr="& strDns2, 0, True


Report •

#2
April 21, 2010 at 09:18:36
✔ Best Answer

Tested with this script. Made some changes...

Dim strIPAddress
Dim strSubnetMask
Dim strGateway
Dim intGatewayMetric
Dim strDns1
Dim strDns2

strIPAddress = "10.10.10.50"
strSubnetMask = "255.255.255.0"
strGateway = "10.10.10.1"
intGatewayMetric = 1
strDns1 = "10.10.10.9"
strDns2 = "10.10.10.10"

Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
objShell.Run "netsh interface ip set dns name=""Local Area Connection"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit

P.S. When running this on a Vista or Windows 7 box you must run it under elevated rights.


Report •

#3
April 21, 2010 at 18:17:43

Thanks, will try it.
Can a login script run a .exe file instead of a .bat file, I was thinking of converting my .bat to .exe

Report •

Related Solutions

#4
April 22, 2010 at 00:20:59

The script does work in admin, and .exe, .cmd, bat, vbs all works in login but can't seem to change the ip address at login using GPO in the login script under users or startup script in computer section, any ideas how I can change the ip when users login as power users since they don't have admin rights to change ip address.

Report •

#5
April 22, 2010 at 10:32:08

Log in scripts run with the authority of the user logging in. If you want the script to change the IP, either give the ability to your users, or run it as a startup script.

Report •

#6
April 22, 2010 at 10:42:53

Yes, I know. This is one thing that I have been complaining bout. Windows does not have an equivalent of a sudo which would be great for login scripts. Probably because this would make a security hole. The reason is even with the runas you have to provide it with the administrator password which would mean it is visible in the login script. I had our programmer make a runasadmin utility which accessed an encrypted password out of our database then executed the runas this way we had full control over the key. There are tools available on the internet that will assist in this...

How to run it with the password clear text:

http://blogs.technet.com/heyscripti...

How to run safely:

http://www.freedownloadscenter.com/...


Report •

#7
April 22, 2010 at 11:05:53

Just a simple question, are you running your DHCP server on a Windows 2003 server? If so why not just use IP Reservations? They ensure a computer gets a specific IP kinda like static IPs but done through DHCP?

This would be way easier than trying to get your script to work.


Report •

#8
April 22, 2010 at 21:37:25

I got it to work now, I just had to link my GPO to the computers, since I am using the startup script under computer configuration.

We are using laptops with docking station which has a LAN port, if the users take it off the dock then they are on wireless, so I wanted fixed IP on the LAN and DHCP on the wireless but we are running out of ip since somehow the LAN ip gets changed to DHCP, which means the laptop gets 2 ips from DHCP server. I tried making the wireless and LAN IP the same but I don't think that works when they dock and undock their laptops even with HO wireless management since they are old laptops and the LAN don't switch off when not in used.


Report •

#9
May 24, 2010 at 01:06:15

I got the script to work but it will only change the IP address if the lan card's name is "Local area connection", what about if it's "local area connection 2" how do I change the script to check for this also? what is an easy way?
Thanks

Report •

#10
June 4, 2010 at 18:26:10

I've been playing a bit with WMI in python and WMIC in
Window's batch. The nicconfig alias should be able to provide
you a bunch of info. Here's the WMIC version:

wmic nicconfig where ipenabled=true get 
/format:list

If VB doesn't allow use of aliases (I'm sure it does), you can
find the target path by using:

wmic alias nicconfig get target

WMI allows use of methods, such as:

WMIC NicConfig where IPEnabled=True CALL 
EnableStatic("192.168.1.55"),("255.255.255.0")

...which would switch a DHCP enabled NIC to have a static IP
address, and providing the subnet mask. Again, I'm pretty
sure VB interfaces with WMI, but I'm not a VB guy...


Report •

#11
June 4, 2010 at 23:04:04

orangeboy: I'm pretty sure VB interfaces with WMI
Indeed it does, in ways much nicer than evoking a shell to run WMIC.

kiwi_hk:
What you really want is static DHCP. It's much nicer than what you're attempting here.


Report •


Ask Question