2014-10-27 129 views
2

我正在尝试编写更改PC的DNS服务器的脚本。关于这一切的难点在于确定Windows给PC的名称。它可以是“本地连接#”的任何迭代。我有一个为WINS服务器执行此操作的脚本,我正试图改变它以更改DNS。该脚本运行但没有做任何事情。没有错误,只是没有任何反应。此脚本的Windows事件日志中没有任何内容正在运行或未运行。任何想法,为什么它运行,但做任何事情?谢谢vbscript更改DNS服务器

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 
+0

代码的下半年应该是改变的DNS服务器完全足够。如果在第二个'strComputer =“。”''之前删除所有内容,是否有什么不起作用?你是否以管理员权限运行这个? – 2014-10-27 20:54:38

+0

我是一个巨大的白痴。我没有作为管理员运行。我期待Windows在这种情况下抛出一个错误。谢谢! – jasonrw 2014-10-28 12:02:45

回答

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

这基本上是OP在发布的代码的后半部分已经做的。 – 2014-10-27 22:53:55

+0

谢谢!这是一个很棒的剧本。 – jasonrw 2014-10-28 12:03:49

0

这VBScript是用于诺顿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 
'**************************************************************************