Announcement

Collapse
No announcement yet.

Need help with this script to change DNS/WINS

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with this script to change DNS/WINS

    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.

    Code:
    On Error Resume Next
     
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    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
    Last edited by Rems; 19th December 2011, 17:47.

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

    well.. where is it failing ?

    sigpic


    Please do show your appreciation to those who assist you by leaving Rep Point

    Comment


    • #3
      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 & "\root\cimv2")
       
      Set colNics = objWMIService.ExecQuery _
          ("Select DeviceID From Win32_NetworkAdapter " _
              & "Where NetConnectionID = " & _
              "'CFN'",,48)
       
      For Each objNic in colNics
         Set colNicConfigs = objWMIService.ExecQuery _
            ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" _
            & objNic.DeviceID & "'}" _
            & " WHERE AssocClass=Win32_NetworkAdapterSetting",,48)
       
         For Each objNicConfig In colNicConfigs
           objNicConfig.SetDNSServerSearchOrder Array(strDNS1, strDNS2)
           objNicConfig.SetWINSServer strWINS1, strWINS2
         Next
      
      Next
      /Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        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
        Marcel
        Technical Consultant
        Netherlands
        http://www.phetios.com
        http://blog.nessus.nl

        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
        "No matter how secure, there is always the human factor."

        "Enjoy life today, tomorrow may never come."
        "If you're going through hell, keep going. ~Winston Churchill"

        Comment


        • #5
          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

          Originally posted by Dumber View Post
          Why not using Netsh.exe? Much easier I think...
          http://www.petri.com/configure_tcp_ip_from_cmd.htm

          Comment


          • #6
            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/0...e-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/l.../ee692588.aspx
            http://technet.microsoft.com/en-us/l.../ee692589.aspx
            Marcel
            Technical Consultant
            Netherlands
            http://www.phetios.com
            http://blog.nessus.nl

            MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
            "No matter how secure, there is always the human factor."

            "Enjoy life today, tomorrow may never come."
            "If you're going through hell, keep going. ~Winston Churchill"

            Comment

            Working...
            X