用于更改DNS服务器的vbscript

时间:2014-10-27 20:29:38

标签: vbscript dns

我正在尝试编写一个更改PC的DNS服务器的脚本。所有这一切的难点在于确定Windows为PC提供的名称。它可以是“Local Area Connection#”的任何迭代。我有一个脚本,为WINS服务器执行此操作,我正在尝试调整它以更改DNS。该脚本运行但不执行任何操作。没有错误,没有任何反应。此脚本中的Windows事件日志中没有任何内容正在运行或未运行。任何想法为什么它运行但确实做了什么?感谢

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

2 个答案:

答案 0 :(得分:1)

我制作了一个vbscript来将我的DNS更改为OpenDNS;所以你可以尝试一下

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
'****************************************************************************************************************

答案 1 :(得分:0)

此Vbscript用于Norton ConnectSave服务:

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
'**************************************************************************