jasonrw
jasonrw

Reputation: 173

vbscript to change DNS server

I am trying to write a script that changes a PC's DNS servers. The hard part about all of this is to determine the name Windows gives the PC. It can be any iteration of "Local Area Connection #". I have a script that does this for the WINS server and I am trying to adapt it to change DNS. The script runs but doesn't do anything. There are no errors, just nothing happens. There isn't anything in the Windows Event Logs around this script running or not running. Any ideas why it runs but does do anything? Thanks

const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
Set ObjWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
  strComputer & "\root\default:StdRegProv")

'Set the path to the Network Interfaces
strKeyPath = "SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces"

'Get all the known interfaces
ObjWMI.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

'If there was a problem getting strKeyPath, exit the script before throwing an error.
If IsNull(arrSubKeys) Then WScript.Quit

strComputer = "."

IPDNS1 = "192.168.1.2"   
IPDNS2 = "192.168.1.3"

arrDNSServers = Array(IPDNS1, IPDNS2)  

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
  objNicConfig.SetDNSServerSearchOrder(arrDNSServers)
Next

Upvotes: 2

Views: 6898

Answers (2)

Hackoo
Hackoo

Reputation: 18857

This Vbscript is used for Norton ConnectSave Service :

Option Explicit
Dim MessageArabe,MessageFr,MessageEn,Titre,Question,strComputer,objWMIService,colNetCards,objNetCard,arrDNSServers
MessageArabe = ChrW(1607)&ChrW(1604)&ChrW(32)&ChrW(1578)&ChrW(1585)&ChrW(1610)&ChrW(1583)&ChrW(32)&_
ChrW(1581)&ChrW(1580)&ChrW(1576)&ChrW(32)&ChrW(1575)&ChrW(1604)&_
ChrW(1605)&ChrW(1608)&ChrW(1575)&ChrW(1602)&ChrW(1593)&ChrW(32)&ChrW(1575)&ChrW(1604)&_
ChrW(1575)&ChrW(1576)&ChrW(1575)&ChrW(1581)&ChrW(1610)&ChrW(1577)&ChrW(32)&ChrW(1608)&_
ChrW(32)&ChrW(1575)&ChrW(1604)&ChrW(1582)&ChrW(1576)&ChrW(1610)&ChrW(1579)&ChrW(1577)
MessageFr = " Voulez-vous bloquer les sites pornographiques et malveillants avec le service NortonConnectSafe ?"
MessageEn = " Do you want to block pornographic and malicious websites with the NortonConnectSafe Service ?"
Titre = MessageArabe &" © Hackoo © 2015"
If AppPrevInstance() Then   
    MsgBox "Il y a une instance déjà en cours",VbExclamation,"Il y a une instance déjà en cours"    
    WScript.Quit   
Else   
    Call ListDNSInfo()
End If   
'****************************************************************************************************************
Question = MsgBox(MessageArabe  & VbcrLf & MessageFr & VbcrLf & MessageEn,vbYesNoCancel+VbQuestion,Titre)
If Question = VbYes then
    Call NortonConnectSafe(True)
End if
If Question = VbNo Then
    Call NortonConnectSafe(False)
Else
    Call ListDNSInfo()
    Wscript.Quit()
End if
'****************************************************************************************************************
Sub NortonConnectSafe(Active)
    Dim OKMsgAR,OKMsgFR,OKMsgEN,NoOKMsgAR,NoOKMsgFR,NoOKMsgEN
    OKMsgAR = ChrW(1578)&ChrW(1605)&ChrW(32)&ChrW(1578)&ChrW(1606)&ChrW(1588)&ChrW(1610)&ChrW(1591)&ChrW(32)&ChrW(1582)&_
    ChrW(1583)&ChrW(1605)&ChrW(1577)&ChrW(32)&ChrW(1571)&ChrW(1576)&ChrW(1606)&ChrW(1583)&ChrW(1606)&ChrW(1587)&ChrW(32)&_
    ChrW(1576)&ChrW(1606)&ChrW(1580)&ChrW(1575)&ChrW(1581)&ChrW(33)

    NoOKMsgAR = ChrW(1578)&ChrW(1605)&ChrW(32)&ChrW(1573)&ChrW(1604)&ChrW(1594)&ChrW(1575)&ChrW(1569)&ChrW(32)&ChrW(1582)&_
    ChrW(1583)&ChrW(1605)&ChrW(1577)&ChrW(32)&ChrW(1571)&ChrW(1576)&ChrW(1606)&ChrW(1583)&ChrW(1606)&ChrW(1587)&ChrW(32)&_
    ChrW(1576)&ChrW(1606)&ChrW(1580)&ChrW(1575)&ChrW(1581)&ChrW(33)

    OKMsgFR = "Le service NortonConnectSafe est activé avec succès !"
    NoOKMsgFR = "Le service NortonConnectSafe est désactivé avec succès !"

    OKMsgEN = "The NortonConnectSafe service is successfully activated !"
    NoOKMsgEN = "The NortonConnectSafe service is off successfully !"
    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")
    If Active = True Then
        For Each objNetCard in colNetCards
            arrDNSServers = Array("184.169.223.35", "199.85.126.30") 'DNS Norton ConnectSafe
            objNetCard.SetDNSServerSearchOrder(arrDNSServers)
            If Err = 0 Then
                MsgBox OKMsgAR & VbCrlF & OKMsgFR & VbCrlF & OKMsgEN,VbInformation, OKMsgAR & OKMsgFR
                Call ListDNSInfo()
            Else
                'MsgBox Err.Description,Vbcritical,Err.Description
            End If  
        Next
    Else
        For Each objNetCard in colNetCards
            objNetCard.SetDNSServerSearchOrder(null)
            If Err = 0 Then
                MsgBox NoOKMsgAR & VbCrlF & NoOKMsgFR & VbCrlF & NoOKMsgEN,VbExclamation,NoOKMsgAR & NoOKMsgFR
                Call ListDNSInfo()
            Else
                'MsgBox Err.Description,Vbcritical,Err.Description
            End If  
        Next
    End If
End Sub
'*****************************************************************************************************************
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
'**************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function   
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************

Upvotes: 0

Hackoo
Hackoo

Reputation: 18857

I made a vbscript to change my DNS to OpenDNS ; so you can give a try

Option Explicit
Dim MessageArabe,MessageFr,MessageEn,Titre,Question,strComputer,objWMIService,colNetCards,objNetCard,arrDNSServers
MessageArabe = ChrW(1607)&ChrW(1604)&ChrW(32)&ChrW(1578)&ChrW(1585)&ChrW(1610)&ChrW(1583)&ChrW(32)&_
ChrW(1581)&ChrW(1580)&ChrW(1576)&ChrW(32)&ChrW(1575)&ChrW(1604)&_
ChrW(1605)&ChrW(1608)&ChrW(1575)&ChrW(1602)&ChrW(1593)&ChrW(32)&ChrW(1575)&ChrW(1604)&_
ChrW(1575)&ChrW(1576)&ChrW(1575)&ChrW(1581)&ChrW(1610)&ChrW(1577)&ChrW(32)&ChrW(1608)&_
ChrW(32)&ChrW(1575)&ChrW(1604)&ChrW(1582)&ChrW(1576)&ChrW(1610)&ChrW(1579)&ChrW(1577)
MessageFr = " Voulez-vous bloquer les sites pornographiques et malveillants ?"
MessageEn = " Do you want to block pornographic and malicious websites ?"
Titre = MessageArabe &" © Hackoo © 2014"
Question = MsgBox(MessageArabe  & VbcrLf & MessageFr & VbcrLf & MessageEn,VbYesNO+VbQuestion,Titre)
If Question = VbYes then
    Call OpenDNS(True)
Else
    Call OpenDNS(False)
End if
'****************************************************************************************************************
Sub OpenDNS(Active)
    Dim OKMsgAR,OKMsgFR,OKMsgEN,NoOKMsgAR,NoOKMsgFR,NoOKMsgEN
    OKMsgAR = ChrW(1578)&ChrW(1605)&ChrW(32)&ChrW(1578)&ChrW(1606)&ChrW(1588)&ChrW(1610)&ChrW(1591)&ChrW(32)&ChrW(1582)&_
    ChrW(1583)&ChrW(1605)&ChrW(1577)&ChrW(32)&ChrW(1571)&ChrW(1576)&ChrW(1606)&ChrW(1583)&ChrW(1606)&ChrW(1587)&ChrW(32)&_
    ChrW(1576)&ChrW(1606)&ChrW(1580)&ChrW(1575)&ChrW(1581)&ChrW(33)

    NoOKMsgAR = ChrW(1578)&ChrW(1605)&ChrW(32)&ChrW(1573)&ChrW(1604)&ChrW(1594)&ChrW(1575)&ChrW(1569)&ChrW(32)&ChrW(1582)&_
    ChrW(1583)&ChrW(1605)&ChrW(1577)&ChrW(32)&ChrW(1571)&ChrW(1576)&ChrW(1606)&ChrW(1583)&ChrW(1606)&ChrW(1587)&ChrW(32)&_
    ChrW(1576)&ChrW(1606)&ChrW(1580)&ChrW(1575)&ChrW(1581)&ChrW(33)

    OKMsgFR = "Le service OpenDNS est activé avec succès !"
    NoOKMsgFR = "Le service OpenDNS est désactivé avec succès !"

    OKMsgEN = "The OpenDNS service is successfully activated !"
    NoOKMsgEN = "The OpenDNS service is off successfully !"
    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")
    If Active = True Then
        For Each objNetCard in colNetCards
            arrDNSServers = Array("208.67.222.123", "208.67.220.123")
            objNetCard.SetDNSServerSearchOrder(arrDNSServers)
            If Err = 0 Then
                MsgBox OKMsgAR & VbCrlF & OKMsgFR & VbCrlF & OKMsgEN,VbInformation, OKMsgAR & OKMsgFR
            Else
                MsgBox Err.Description,Vbcritical,Err.Description
            End If  
        Next
    Else
        For Each objNetCard in colNetCards
            objNetCard.SetDNSServerSearchOrder(null)
            If Err = 0 Then
                MsgBox NoOKMsgAR & VbCrlF & NoOKMsgFR & VbCrlF & NoOKMsgEN,VbExclamation,NoOKMsgAR & NoOKMsgFR 
            Else
                MsgBox Err.Description,Vbcritical,Err.Description
            End If  
        Next
    End If
End Sub
'****************************************************************************************************************

Upvotes: 1

Related Questions