Saturday, January 28, 2006

Script to Set DNS

When I spoke with coworkers about this script they asked that since I control DHCP in the network, why should I manually assign these addresses? Every admin out there has seen a computer take 1 minute getting an address from dhcp. Plus, as the primary address is their local domain dns and the second address is a widely used web dns server (207.217.120.83 sprints I believe) their should be no situation where they are required to change name servers.
The first problem I ran into with this script is that there appears to be no easy way to assign addressing via vbscript without directly accessing the registry. However a quick batch file should be able to drop to netsh and assign this manually. While attempting to use the Wscript.Shell.Run to run each command via the shell I ran into a problem which I believe is related to the speed with which the commands are being processed. Therefore I am going to attempt to create a batch file which calls netsh and process the commands from there.
I searched the web for some hints and came to this reference on programmaticly displaying the settings of network adapters. Based on this I was able to create a batch file containing the below content:
netsh int ip set dns name="Local Area Connection" source=static addr=192.168.1.10 register=PRIMARY
netsh int ip add dns name="Local Area Connection" addr=207.217.120.83 index=2
The second command in this script threw me for a loop. It seems counter intuitive to require separate objects to set a primary and a secondary name server. To finalize this script I include a Wscript.Shell.Run call in a vbscript to the batch file.
Set wshShell = WScript.CreateObject("WScript.Shell")
nReturnCode = wshShell.Run("setdns.bat")

0 Comments:

Post a Comment

<< Home