Need help setting DNS settings via VBS.

Home Forums Scripting General Scripting Need help setting DNS settings via VBS.

This topic contains 5 replies, has 3 voices, and was last updated by Avatar themanwhowouldbeking 11 years, 8 months ago.

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

    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

    Quote:
    On Error Resume Next
    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)
    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

    #297993

    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 & “rootcimv2”)

    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

    #296823

    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,

    themanwhowouldbeking;95112 wrote:
    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 & “rootcimv2”)

    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

    #297994

    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_MACHINESYSTEMCurrentControlSetServicesTcpipParametersInterfaces
    -r 192.168.1.77
    #296824

    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.

    On Error Resume Next

    strComputer = “.”
    strWINSPrimaryServer = “192.168.0.1”
    strWINSSecondaryServer = “192.168.0.2”

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
    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 & “rootcimv2”)
    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
    [/CODE][CODE]
    On Error Resume Next

    strComputer = “.”
    strWINSPrimaryServer = “192.168.0.1”
    strWINSSecondaryServer = “192.168.0.2”

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
    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 & “rootcimv2”)
    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
    [/CODE]

    Rems
    Rems
    Moderator
    #226771

    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/Miscellaneous/Q_22002013.html

    http://www.microsoft.com/technet/scriptcenter/topics/networking/05_atnc_dns.mspx

    Rems

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

You must be logged in to reply to this topic.