djsnakz
djsnakz

Reputation: 478

VBscript to target specific IP of NIC and change its DNS settings

I have currently been tasked to put together a script that will change the DNS settings of 15,000 ish servers. However, there is no common unique identifer of these NIC's other than their current DNS IP. My Question, Is it possible to somehow have my script do an ipfonfig /all and then if one of the NIC's reports back with the current DNS settings target that NIC for the new updated settings?

I was currently using the below script until i was made aware that some of the NIC will not be called "Production". Any suggestions are welcome! (powershell was not an option as we may be targeting some very old servers)

Dim strDns1
Dim strDns2

strDns1 = "10.10.10.10"
strDns2 = "10.10.10.10"

Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set dns name=""Production"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Production"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit

Upvotes: 1

Views: 1309

Answers (2)

Hackoo
Hackoo

Reputation: 18847

I'm agree with Syberdoor , You should use the Wmi Class

Win32_NetworkAdapterConfiguration

This code give you some informations :

Call ListDNSInfo()
'********************************************************************
Sub ListDNSInfo()
    Dim ComputerName,IPConfigSet,IPConfig,BailObtenu,BailExpirant
    ComputerName="."
    On error resume next 
    set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & ComputerName).ExecQuery _ 
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE") 
    If Err.Number <> 0 Then 
        wscript.echo " - non accessible -" 
    Else 
        for each IPConfig in IPConfigSet 
            BailObtenu = IPConfig.DHCPLeaseObtained
            BailExpirant = IPConfig.DHCPLeaseExpires
'---- Convertion des date et heure d'obtention et d'expiration des baux DHCP en un format lisible par l'utilisateur. ----
            BailObtenu = mid(BailObtenu, 7, 2) & "/" & mid(BailObtenu, 5, 2) & "/" & mid(BailObtenu, 1, 4) & " - " & mid(BailObtenu, 9, 2)& ":" & mid(BailObtenu, 11, 2)& ":" & mid(BailObtenu, 13, 2)
            BailExpirant = mid(BailExpirant, 7, 2) & "/" & mid(BailExpirant, 5, 2) & "/" & mid(BailExpirant, 1, 4) & " - " & mid(BailExpirant, 9, 2)& ":" & mid(BailExpirant, 11, 2)& ":" & mid(BailExpirant, 13, 2)

            MsgBox " Configuration réseau de l'ordinateur " & ComputerName & vbcrlf & vbcrlf & _ 
            "Nom Machine " & vbtab & " : " & IPConfig.DNSHostName & vbcrlf & _ 
            "Carte active" & vbtab & " : " & IPConfig.Description & vbcrlf & _ 
            "Adresse MAC " & vbtab & " : " & IPConfig.MACAddress & vbcrlf & _ 
            "DHCP Activé" & vbtab & " : " & IPConfig.DHCPEnabled & vbcrlf & _
            "Adresse IP " & vbtab & " : " & IPConfig.IPAddress(0) & vbcrlf & _
            "Masque " & vbtab & vbtab & " : " & IPConfig.IPSubnet(0) & vbcrlf & _
            "Passerelle     " & vbtab & " : " & IPConfig.DefaultIPGateway(0) & vbcrlf & _   
            "Serveur DHCP " & vbtab & " : " & IPConfig.DHCPServer & vbcrlf & vbcrlf & _
            "Serveur DNS " & vbtab & " : " & IPConfig.DNSServerSearchOrder(0) & vbcrlf & _
            "             " & vbtab & vbtab & " : " & IPConfig.DNSServerSearchOrder(1) & vbcrlf & _
            "Serveur WINS " & vbtab & " : " & IPConfig.WINSPrimaryServer(0) & vbcrlf & _
            "             " & vbtab & vbtab & " : " & IPConfig.WINSSecondaryServer(0) & vbcrlf & vbcrlf & _
            " Bail obtenu " & vbtab & " : " & BailObtenu & vbcrlf & _
            " Bail expirant " & vbtab & " : " & BailExpirant _                    
            ,VbInformation,"Configuration réseau de l'ordinateur " 
        Next 
    End If
End Sub

Upvotes: 0

Syberdoor
Syberdoor

Reputation: 2619

You can do the ipconfig query with a script like this:

Set wso = CreateObject("WScript.Shell") 

Set execo = wso.Exec("ipconfig /all")
Set stdout = execo.StdOut
While Not stdout.AtEndOfStream
    cmdOutput = cmdOutput & VbCrLf  & stdout.ReadLine
Wend

wscript.echo cmdOutput

The main problem is this is just a string, so you have to parse it yourself, which is annoying and probably error prone.

I would rather suggest you take a look at the wmi class

Win32_NetworkAdapterConfiguration

which has a lot of information on the network connections. Only caveat with this WMI class is that it stores many values in arrays instead of strings so you can not just query for everything easily. Either pick something to query against that is a string like DNSDomain or just handle the logic within the script.

Upvotes: 0

Related Questions