Need help with this script to change DNS/WINS

Home Forums Scripting Windows Script Host Need help with this script to change DNS/WINS

This topic contains 5 replies, has 4 voices, and was last updated by tehcamel tehcamel 7 years, 10 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #157206

    Hello,
    I have been working on this script for a couple and can’t seen to get it to work. All the parts of the script seem valid. If anyone can lend an Eye that would be awesome. Suggestings are welcome.

    I am trying to:
    1. Set DNS and WINS
    2. Apply it only to a named NIC
    3. Would love to have some error logging.

    Here is the script.

    On Error Resume Next

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    strDNS1 = “192.168.20.150”
    strDNS2 = “192.168.20.41”
    strWins1 = “192.168.20.150”
    strWins2 = “192.168.20.41”

    Set colNicConfigs = objWMIService.ExecQuery _
    (“SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = ‘CFN'”,,48) < - This is ,,fortyeight) For Each objNicConfig In colNicConfigs arrDNSServers = Array(strDNS1, strDNS2) objNicConfig.SetDNSServerSearchOrder(arrDNSServers) intSetWINS = objNicConfig.SetWINSServer(strWINS1, strWINS2) 'If intSetWINSServer = 0 Then 'WScript.Echo "Success! WINS & DNS servers configured." 'ElseIf intSetWINSServer = 1 Then 'WScript.Echo "WINS & DNS servers configured, please reboot." 'Else 'WScript.Echo "Error!! Unable to configure WINS & DNS servers." 'End If Next [/CODE][CODE]
    On Error Resume Next

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    strDNS1 = “192.168.20.150”
    strDNS2 = “192.168.20.41”
    strWins1 = “192.168.20.150”
    strWins2 = “192.168.20.41”

    Set colNicConfigs = objWMIService.ExecQuery _
    (“SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = ‘CFN'”,,48) < - This is ,,fortyeight) For Each objNicConfig In colNicConfigs
    arrDNSServers = Array(strDNS1, strDNS2)
    objNicConfig.SetDNSServerSearchOrder(arrDNSServers)
    intSetWINS = objNicConfig.SetWINSServer(strWINS1, strWINS2)
    ‘If intSetWINSServer = 0 Then
    ‘WScript.Echo “Success! WINS & DNS servers configured.”
    ‘ElseIf intSetWINSServer = 1 Then
    ‘WScript.Echo “WINS & DNS servers configured, please reboot.”
    ‘Else
    ‘WScript.Echo “Error!! Unable to configure WINS & DNS servers.”
    ‘End If
    Next
    [/CODE]

    tehcamel
    tehcamel
    Moderator
    #358415

    Re: Need help with this script to change DNS/WINS

    well.. where is it failing ?

    Rems
    Rems
    Moderator
    #228105

    Re: Need help with this script to change DNS/WINS

    The object does not support the SetDNSServerSearchOrder method because the Win32_NetworkAdapter class does support the SetDNSServerSearchOrder and the SetWINSServer methods.

    The Win32_NetworkAdapter stores network connection information while IP configurations are stored in the Win32_NetworkAdapterConfiguration class.

    The Win32_NetworkAdapterConfiguration class does support the methods.
    But then again, you won’t be able to get the adapter’s name for comparison from the Win32_NetworkAdapterConfiguration class.

    Bij using one of the properties of a certain class that also exist and is the same in an other class you are able to associate.

    sample:

    Code:
    strDNS1 = “192.168.20.150”
    strDNS2 = “192.168.20.41”
    strWins1 = “192.168.20.150”
    strWins2 = “192.168.20.41”

    strComputer = “.”

    Set objWMIService = GetObject( _
    “winmgmts:\” & strComputer & “rootcimv2”)

    Set colNics = objWMIService.ExecQuery _
    (“Select DeviceID From Win32_NetworkAdapter ” _
    & “Where NetConnectionID = ” & _
    “‘[COLOR=”Navy”]CFN[/COLOR]'”,,48)

    For Each objNic in colNics
    Set colNicConfigs = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='” _
    & objNic.DeviceID & “‘}” _
    & ” WHERE [B]AssocClass[/B]=Win32_NetworkAdapterSetting”,,48)

    For Each objNicConfig In colNicConfigs
    objNicConfig.SetDNSServerSearchOrder Array(strDNS1, strDNS2)
    objNicConfig.SetWINSServer strWINS1, strWINS2
    Next

    Next

    /Rems

    Avatar
    Dumber
    Participant
    #201799

    Re: Need help with this script to change DNS/WINS

    Why not using Netsh.exe? Much easier I think…
    http://www.petri.com/configure_tcp_ip_from_cmd.htm

    #296827

    Re: Need help with this script to change DNS/WINS

    Good Morning Dumber,
    I did use a vbscript that read a txt file of servers and copied a batchfile that had the NetSH commands that worked but the difference between Windows 2000, 2003 and 2008 are a pain.

    I couldn’t get NetSH to work on Windows 2000 at all but managed to get 2003/2003 to. This required a lot of work and I think in this large of an environment a pure VBscript would have been better. Total server 834.

    You can see the differences below with NetSH:

    Windows 2008
    netsh interface ip set dns “CFN” static 171.74.74.155 primary
    netsh interface ip add dnsserver “CFN” 171.74.107.198 index=2

    netsh interface ip set wins “CFN” static 171.74.163.196
    netsh interface ip add winsserver “CFN” 171.74.107.15 index=2

    Windows 2003
    netsh interface ip set dns “CFN” static 171.74.74.155 primary
    netsh interface ip add dns “CFN” 171.74.107.198 2

    netsh interface ip set wins “CFN” static 171.74.107.15 primary
    netsh interface ip add wins “CFN” 171.74.163.196 2

    Thanks,
    David

    Dumber;251120 wrote:
    Why not using Netsh.exe? Much easier I think…
    http://www.petri.com/configure_tcp_ip_from_cmd.htm
    Avatar
    Dumber
    Participant
    #201802

    Re: Need help with this script to change DNS/WINS

    Ah you didn’t say that ;)

    And what about Powershell? This should work for at least all your windows 2003/2008 servers.
    http://fatbeards.blogspot.com/2008/06/change-dnswins-ip-on-multiple-servers.html

    Ok, Windows 2000 is nowadays a pain in the ass anyway :)
    You might follow the following guides for using WMI for the good old servers.
    http://technet.microsoft.com/en-us/library/ee692588.aspx
    http://technet.microsoft.com/en-us/library/ee692589.aspx

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.