Announcement

Collapse
No announcement yet.

Need help setting DNS settings via VBS.

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

  • Need help setting DNS settings via VBS.

    I have been trying to get a VBS script written to change DNS entries on local servers but have been having mad problems. The first problem is deleting all old entries.

    Ok I have found this script below but the problems I will run into is that there are 2 active connections, one that needs to be changed and the other that should be changed at all.

    LAN1
    LAN2

    On Error Resume Next
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colNetCards = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    For Each objNetCard in colNetCards
    arrDNSServers = Array("192.168.1.77")
    objNetCard.SetDNSServerSearchOrder(arrDNSServers)
    Next
    Can anyone give me advise or point me in a better direction?
    Thanks,
    Certifiedgeek05
    Last edited by certifiedgeek05; 22nd February 2008, 18:45.

  • #2
    Re: Need help setting DNS settings via VBS.

    I would use the below, I have just used this to modify the DNS servers on a virtual machine and then to modify the DNS server to the original IP address. The only difference from your visual basic script is that I use the impersonate value as the impersation level to use the logon credentials of the user executing the visual basic script, this is the recommened impersonation level when using WMI scripts.

    Code:
    On Error Resume Next
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colNetCards = objWMIService.ExecQuery _
        ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    
    For Each objNetCard in colNetCards
        arrDNSServers = Array("192.168.1.77")
        objNetCard.SetDNSServerSearchOrder(arrDNSServers)
    Next
    MCSA 2000/2003

    Comment


    • #3
      Re: Need help setting DNS settings via VBS.

      Hello themanwhowouldbeking,
      I appreciate the help! My only problem is there are 2 network cards, one needs the DNS changes and the other is on the DMZ so no change required or needed.

      I think I am getting closer, I could do this so much easier in VB6 but then the client would require a lot to make sure the application is safe in their evironment.

      I will post what I add/find incase someone needs help with this also.

      Thank you,

      Originally posted by themanwhowouldbeking View Post
      I would use the below, I have just used this to modify the DNS servers on a virtual machine and then to modify the DNS server to the original IP address. The only difference from your visual basic script is that I use the impersonate value as the impersation level to use the logon credentials of the user executing the visual basic script, this is the recommened impersonation level when using WMI scripts.

      Code:
      On Error Resume Next
      
      strComputer = "."
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      
      Set colNetCards = objWMIService.ExecQuery _
          ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
      
      For Each objNetCard in colNetCards
          arrDNSServers = Array("192.168.1.77")
          objNetCard.SetDNSServerSearchOrder(arrDNSServers)
      Next

      Comment


      • #4
        Re: Need help setting DNS settings via VBS.

        If you know the GUID of the network interface card you could use regfind from the Windows Resource Kit to find and replace the DNS Server IP address by using the following command:

        Code:
        regfind -p HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\<GUID>
        <oldipaddress> -r 192.168.1.77
        MCSA 2000/2003

        Comment


        • #5
          Re: Need help setting DNS settings via VBS.

          Hey Guys,
          I am getting closer...

          I have been working with this script but the function only replaces one DNS entry. I have modified this script a little to hold 4 variables. The script doesn't error at all just doesn't make the second change.

          strOld1DNSServer = "10.17.160.172"
          strNew1DNSServer = "192.168.0.2"
          strOld2DNSServer = "10.17.160.173"
          strNew2DNSServer = "192.168.0.4"

          I am just having a problem getting it to work. Any help would be awesome.

          Code:
          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")
           
           
          For Each objNicConfig In colNicConfigs
           
            intSetWINSServer = objNicConfig.SetWINSServer(strWINSPrimaryServer, _
             strWINSSecondaryServer)
            
          Next
           
          
           
          Set colNicConfigs = objWMIService.ExecQuery _
           ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
           
          
          On Error Resume Next
           
          strComputer = "."
          'strOldDNSServer = "10.17.160.172"
          'strNewDNSServer = "192.168.0.1"
          
          strOld1DNSServer = "10.17.160.172"
          strNew1DNSServer = "192.168.0.2"
          strOld2DNSServer = "10.17.160.173"
          strNew2DNSServer = "192.168.0.4"
           
          Set objWMIService = GetObject("winmgmts:" _
           & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
          Set colNicConfigs = objWMIService.ExecQuery _
           ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
           
          
           
          For Each objNicConfig In colNicConfigs
            
            arrDNSServerSearchOrder = objNicConfig.DNSServerSearchOrder
            
            If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
              For Each strDNSServer In objNicConfig.DNSServerSearchOrder
                
              Next
            End If
            blnFound = 0
            For i = 0 to UBound(arrDNSServerSearchOrder)
              If arrDNSServerSearchOrder(i) = strOld1DNSServer Then
                arrDNSServerSearchOrder(i) = strNew1DNSServer
                blnFound = 1
              End If
            Next
            If blnFound Then
              retSetDNS = objNicConfig.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
              
            Else
              
            End If
          Next
          For Each objNicConfig In colNicConfigs
            
            arrDNSServerSearchOrder = objNicConfig.DNSServerSearchOrder
            
            If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
              For Each strDNSServer In objNicConfig.DNSServerSearchOrder
                
              Next
            End If
            blnFound = 0
            For i = 0 to UBound(arrDNSServerSearchOrder)
              If arrDNSServerSearchOrder(i) = strOld2DNSServer Then
                arrDNSServerSearchOrder(i) = strNew2DNSServer
                blnFound = 1
              End If
            Next
            If blnFound Then
              retSetDNS = objNicConfig.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
              
            Else
              
            End If
          Next 
          
           
          Set colNicConfigs = objWMIService.ExecQuery _
           ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
           
          For Each objNicConfig In colNicConfigs
           
            If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
              For Each strDNSServer In objNicConfig.DNSServerSearchOrder
                
              Next
            End If
          Next
          Last edited by certifiedgeek05; 12th March 2008, 16:18.

          Comment


          • #6
            Re: Need help setting DNS settings via VBS.

            You can only add the new DNS Search order as a complete array when using the 'SetDNSServerSearchOrder' method. After you first pas an empty array to delete the existing IPaddresses.

            You can identify the right networkadapter by checking the existens of one of the old DNSServerIPaddress.

            Have a look also at this thread;
            http://forums.petri.com/showthread.php?t=17235


            In cases the right networkadapter on every computer has several more ipaddresses, in a different order on every computer - and you only want to replace a few ipaddresses without knowing the other addresses, then you must; 1.read the existing DNSServerSearchOrder to an array. 2.Update the array (using ReDim Preserve). 3.Clear the existing DNSServerSearchOrder. 4.Pas the new array.

            http://www.experts-exchange.com/OS/M..._22002013.html

            http://www.microsoft.com/technet/scr..._atnc_dns.mspx

            \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

            Working...
            X