Link to home
Start Free TrialLog in
Avatar of Who Dat
Who DatFlag for United States of America

asked on

Update WINS entries throughout domain

Some EE genius just assisted me w/updating my preferred DNS entries via a vbs script.  

What I would like to do now is run a similar script to change the WINS entries.  There was one DNS/WINS server that had an IP change and I need to update the WINS entries to point to the new IP address.

If there's a script as easy as my DNS question, that would be perfect!
https://www.experts-exchange.com/questions/23778819/Update-Preferred-DNS-throughout-Domain.html?cid=748&anchorAnswerId=22664381#a22664381

Thanks!
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

"Stolen" from http://www.microsoft.com/germany/technet/datenbank/articles/600486.mspx
On Error Resume Next
 
strComputer = "."
strWINSPrimaryServer = "192.168.0.1"
strWINSSecondaryServer = "192.168.0.2"
 
 Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
 WScript.Echo VbCrLf & "Host Name: " & strComputer & VbCrLf & _
 "  Attempting to set WINS primary and secondary servers ..."
 
 For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description
  intSetWINSServer = objNicConfig.SetWINSServer(strWINSPrimaryServer, _
   strWINSSecondaryServer)
  If intSetWINSServer = 0 Then
    WScript.Echo "    Successfully set WINS servers."
  ElseIf intSetWINSServer = 1 Then
    WScript.Echo "    Successfully set WINS servers." & _
     VbCrLf & "    Must reboot."
  Else
    WScript.Echo "    Unable to set WINS servers."
  End If
Next
 
 WScript.Echo VbCrLf & String(80, "-")
 
 Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
 For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description
  WScript.Echo "    Primary WINS Server: " & objNicConfig.WINSPrimaryServer
  WScript.Echo "    Secondary WINS Server: " & _
   objNicConfig.WINSSecondaryServer
Next

Open in new window

Avatar of Who Dat

ASKER

How does this code work?
Does it scan AD or can I request it pull the server names via a text file?  And also I do not want the servers to reboot.
Warning: This is the above code worked into my AD routine, not my code.
Warning: There's no DRYRUN
Warning: It replaces ALL WINS servers on all servers in the OU, as in, it does things "the easy way" and assumes all your servers in that OU use exactly the same (new) WINS servers

If you want I can mod the old DNS script to update WINS, and give you full DRYRUN functionality and matched replace, but it was so much faster to do this way.
'New WINS Servers
strWINSPrimaryServer = "192.168.0.1"
strWINSSecondaryServer = "192.168.0.2"
 
'user parameter - domain path
'example:  LDAPPath="LDAP://OU=Domain Controllers,dc=mydomain,dc=com"
'example2: LDAPPath="LDAP://CN=Computers,dc=mydomain,dc=co,dc=uk"
LDAPPath="LDAP://OU=Corp Servers,OU=US Servers,dc=us,dc=ad,dc=united,dc=com"
 
'user parameter - number of pings before considering a host to be dead - should probably be at least 2 in a production script, but this should work fine
pingno=1
 
On Error Resume Next
 
Set objOU = GetObject(LDAPPath)
objOU.Filter = Array("Computer")
 
For Each objComputer in objOU
strComputer = objComputer.CN
 
Set objShell = CreateObject("WScript.Shell")
 
strCommand = "ping -n " & pingno & " " & strComputer
 
Return = objShell.Run(strCommand,0,True)
 
If Return = 0 Then
 
' =====================================================================
 
' Insert your query code here
 
' =====================================================================
 On Error Resume Next
 
 Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
 WScript.Echo VbCrLf & "Host Name: " & strComputer & VbCrLf & _
 "  Attempting to set WINS primary and secondary servers ..."
 
 For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description
  intSetWINSServer = objNicConfig.SetWINSServer(strWINSPrimaryServer, _
   strWINSSecondaryServer)
  If intSetWINSServer = 0 Then
    WScript.Echo "    Successfully set WINS servers."
  ElseIf intSetWINSServer = 1 Then
    WScript.Echo "    Successfully set WINS servers." & _
     VbCrLf & "    Must reboot."
  Else
    WScript.Echo "    Unable to set WINS servers."
  End If
Next
 
 WScript.Echo VbCrLf & String(80, "-")
 
 Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
 For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description
  WScript.Echo "    Primary WINS Server: " & objNicConfig.WINSPrimaryServer
  WScript.Echo "    Secondary WINS Server: " & _
   objNicConfig.WINSSecondaryServer
Next
 
 
' =====================================================================
 
' End Query code
 
' =====================================================================
 
 
Else
Wscript.StdOut.WriteLine vbTab & "NETWORK ERROR"
End If
Next

Open in new window

as far as I know a change of the WINS configuration won't go into effect without rebooting.
The script knows otherwise, if you read it - it will wanr you if you need to reboot for the configuration to take effect.
It does NOT reboot the servers by itself, but you might like to make a note which ones need to be rebooted and schedule them for 5 min downtime.
Avatar of Who Dat

ASKER

sciphre, the easiest thing I could or would like to do, is pull the servers from the servers.txt file.  For some reason the servers OU is broken up into about 10 subsets.  Can you incorporate the txt file into the above script?
This should do the trick. Same format for servers.txt as the other one - one per line, last line empty.
'New WINS Servers
strWINSPrimaryServer = "192.168.0.1"
strWINSSecondaryServer = "192.168.0.2"
 
'user parameter - number of pings before considering a host to be dead - should probably be at least 2 in a production script, but this should work fine
pingno=1
 
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("servers.txt",1)
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
 
Set objShell = CreateObject("WScript.Shell")
 
strCommand = "ping -n " & pingno & " " & strComputer
 
Return = objShell.Run(strCommand,0,True)
 
If Return = 0 Then
 
' =====================================================================
 
' Insert your query code here
 
' =====================================================================
 On Error Resume Next
 
 Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
 WScript.Echo VbCrLf & "Host Name: " & strComputer & VbCrLf & _
 "  Attempting to set WINS primary and secondary servers ..."
 
 For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description
  intSetWINSServer = objNicConfig.SetWINSServer(strWINSPrimaryServer, _
   strWINSSecondaryServer)
  If intSetWINSServer = 0 Then
    WScript.Echo "    Successfully set WINS servers."
  ElseIf intSetWINSServer = 1 Then
    WScript.Echo "    Successfully set WINS servers." & _
     VbCrLf & "    Must reboot."
  Else
    WScript.Echo "    Unable to set WINS servers."
  End If
Next
 
 WScript.Echo VbCrLf & String(80, "-")
 
 Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
 For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description
  WScript.Echo "    Primary WINS Server: " & objNicConfig.WINSPrimaryServer
  WScript.Echo "    Secondary WINS Server: " & _
   objNicConfig.WINSSecondaryServer
Next
 
 
' =====================================================================
 
' End Query code
 
' =====================================================================
 
 
Else
Wscript.StdOut.WriteLine vbTab & "NETWORK ERROR"
End If
Loop

Open in new window

Avatar of Who Dat

ASKER

the servers have anywhere between 2 to 4 DNS and WINS entries.  What I want to do is modify the outdated server IP with the new server IP so I don't want to modify any other entries and I'm not certain in what order the old entry is set, whether it's the first, second, third or forth.  It want that old value found, say 192.168.1.1 and changed to 192.168.151.1
Then the old script needs remade. I'll try to do it today.
WMI doesn't have the concept of tertiary and quaternary WINS servers I'm afraid.
Doesn't work like DNS did - can't even query them that are beyond 2.
Avatar of Who Dat

ASKER

so what type of options do I have?
If I run the script and set primary, secondary, third and fourth, will it update all the entries or will it ADD to the entries to where there could be duplicates?
It will SET the primary and the secondary, and with some work they can be "set if previous is wrong" as the DNS script did.
Absolutely no info on the third and fourth, could be anything, but you just can't find out via WMI.

The option i can think of that may work reasonably well is to run a netsh script on all servers (which can be done via a VBScript that calls psExec on all of them) and set the third and fourth WINS, but you still wouldn't be able to mix and match - it's set or don't set, can't "set if previous is wrong" automatically without somehow parsing previous output from netsh, which is over my head.

Could however get netsh to read you the old WINS into a file and you'd parse that with the ol'brain.

Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run "psexec """ & strComputer & " netsh >> c:\netshlog.txt"

We could run that remotely on all servers and then you'd read it and decide which need changing what, and these can be updated manually with the following commands:
Note however that it would STILL be sequential, you can't "update WINS4" you need to "set only one WINS, 192.168.1.1" then "add" three more.

psexec \\fqdn "netsh interface ip set wins name="Local Area Connection" static 192.168.1.1"
psexec \\fqdn "netsh interface ip add wins name="Local Area Connection" 192.168.1.2"
psexec \\fqdn "netsh interface ip add wins name="Local Area Connection" 192.168.1.3"
psexec \\fqdn "netsh interface ip add wins name="Local Area Connection" 192.168.1.4"

The whole thing owuld save you from needing to remote desktop into each box, but there's no simple way i can think of like we did with DNS.
Also, I'll be calling it a day any minute now.
Avatar of Who Dat

ASKER

ok, so what can I run to see what the current settings are?  and it was easier pulling the server names from the txt file...
not familiar w/netsh
Avatar of Who Dat

ASKER

I receive this error when I run netsh:
netsh>WshShell.Run "psexec """ & strComputer & " netsh >> c:\netshlog.txt"
The following command was not found: WshShell.Run "psexec """ & strComputer & "
netsh >> c:\netshlog.txt".

and I do have psexec
That won't work without the VBScript wrapper, but I didn't have time to get the details down yesterday.
Here, try this, should give you a big list of servers and wins entries for all their cards - note the keyword ALL - including VMWare interfaces and others:
pingno=1
 
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("servers.txt",1)
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
 
Set objShell = CreateObject("WScript.Shell")
 
strCommand = "ping -n " & pingno & " " & strComputer
 
Return = objShell.Run(strCommand,0,True)
 
If Return = 0 Then
 
' =====================================================================
 
' Insert your query code here
 
' =====================================================================
 
 Set WshShell = Wscript.CreateObject("Wscript.Shell")
 WshShell.Run "psexec \\" & strComputer & " ""netsh interface ip show wins"" >> c:\netshlog.txt"
 
' =====================================================================
 
' End Query code
 
' =====================================================================
 
 
Else
Wscript.StdOut.WriteLine vbTab & "NETWORK ERROR"
End If
Loop

Open in new window

Avatar of Who Dat

ASKER

ok, I started the run using cscript + vbs and the psexec windows just wouldn't stop - anwyay of running it silent w/out the windows?
Yes, there is.
I also changed it to wait for each psExec to finish or you will probably not be able to match computer names to settings... that'd be a shame.
You'll need to have psexec in the folder, forgot to mention that too.
pingno=1
 
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("servers.txt",1)
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
 
Set objShell = CreateObject("WScript.Shell")
 
strCommand = "ping -n " & pingno & " " & strComputer
 
Return = objShell.Run(strCommand,0,True)
 
If Return = 0 Then
 
' =====================================================================
 
' Insert your query code here
 
' =====================================================================
 
 Set objShell2 = CreateObject("Wscript.Shell")
 strCommand = "echo " & strComputer & " >> c:\netshlog.txt"
 Return = objshell2.Run(strCommand,0,True)
 strCommand = "psexec.exe \\" & strComputer & " ""netsh interface ip show wins"" >> c:\netshlog.txt"
 Return = objShell2.Run(strCommand,0,True)
 
' =====================================================================
 
' End Query code
 
' =====================================================================
 
 
Else
Wscript.StdOut.WriteLine vbTab & "NETWORK ERROR"
End If
Loop

Open in new window

Avatar of Who Dat

ASKER

sciphre, I ran the code yesterday and it just sat there for a few hours with no output.
Well, yes, but you should have the output logged in the c:\netshlog.txt file

In theory.
Avatar of Who Dat

ASKER

the output file isn't being generated and I've confirmed it hasn't been placed outside the root of C: drive
What a stupid bug. Really sorry, I worked with several versions of the file and them melded the wrong ones in, and made a dumb assumption to boot.
This one's tested this time, and works well if psexec is in your path or working dir.
I've also done the not dumb thing and removed the absolute path from netshlog.txt, it will create it in your working directory.
netshlog is a bit verbose as i don't know how to filter out the non-physical interfaces from it's output as I was doing with WMI - to be honest i hate working with text, give me objects any day.

I've been really busy the last few days, sorry to keep you waiting - also, don't forget that there will be no script solution to conditionally write the new WINS servers.
It can be done (more accurately, I can do it) if you accept to replace all 4 WINS servers on the targets, but not otherwise.
ASKER CERTIFIED SOLUTION
Avatar of sciphre
sciphre
Flag of Romania 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 Who Dat

ASKER

a ha! there we go now...
Avatar of Who Dat

ASKER

oh, and I would be interested in this:
"It can be done (more accurately, I can do it) if you accept to replace all 4 WINS servers on the targets, but not otherwise"

Most of the servers should have four entries, but some only have two.  If I could wipe all the entries and start from scratch, that would be perfect!
If we ignore previous entries and write 4 it can be done, just say the word
Avatar of Who Dat

ASKER

word
I'll try to do it tomorrow, walked into a complication that happened to give me a good idea, but I'm too tired to do it at the mo.
Avatar of Who Dat

ASKER

sciphre, have you had any time to work on the code?
I've played with it a bit, it currently seems to work but will attempt to set wins for all interfaces, not just the physical ones (vmware networks in my case).
Is this ok ?
Avatar of Who Dat

ASKER

that should be fine..