2014-10-27 143 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 
'**************************************************************************