.vbs脚本更改连接特定的DNS后缀

时间:2014-09-10 15:41:04

标签: vbscript dns cmd command

我正在寻找一种方法让.vbs文件为已知名称为LAN的以太网适配器添加特定于连接的DNS后缀

代码形成了一个小的shell脚本:

  1. 更改主DNS后缀
  2. 刷新DNS
  3. 检查IP详细信息以确保所有内容都已保留
  4. 更改启动类型或服务
  5. 启动说服务
  6. ping知道服务器以确保网络连接
  7. 似乎找不到任何能够使第一步工作的代码。目前我有:

     'add dns
    
    Const HKEY_LOCAL_MACHINE = &H80000002
    strComputer = "."
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
    
    strKeyPath = "SOFTWARE\Policies\Microsoft\Windows NT\DNSClient"
    oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
    strValueName = "AppendToMultiLabelName"
    'Enabled
    strValue = "mysuffix.com"
    oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
    
    
    'flush dns
    
    set shell = createobject("wscript.shell") 
    shell.run("ipconfig /flushdns")
    Wscript.echo "DNS Flushed."
    
    
    'check DNS
    
    Set objShell = CreateObject("WScript.Shell")
    Set objScriptExec = objShell.Exec("ipconfig /all")
    strIpConfig = objScriptExec.StdOut.ReadAll
    WScript.Echo strIpConfig
    
    
    'start McAfee
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name = 'enterceptAgent'")
    
    For Each objService in colServiceList
        If objService.State = "Stopped" Then
            objService.StartService()
            Wscript.Sleep 5000
            Wscript.Echo "McAfee Started"
        Else
            Wscript.Echo "McAfee already running"
            Wscript.Sleep 5000
        End If
        errReturnCode = objService.ChangeStartMode("automatic")   
    Next
    
    'network test
    
    set shell = createobject("wscript.shell") 
    shell.run("ping pmo2 -t")
    

    干杯

1 个答案:

答案 0 :(得分:0)

尝试此操作(配置为正确的GUID,请参阅objNetCard.SettingID属性):

option explicit
'On Error Resume Next
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName

Dim strComputer, objWMIService, colNetCards, objNetCard
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colNetCards = objWMIService.ExecQuery _ 
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True") 

For Each objNetCard in colNetCards
  strResult = strResult _
    & vbNewLine & objNetCard.Caption & vbTab & "'" & objNetCard.DNSDomain & "'" & vbTab & CBool( IsEmpty( objNetCard.DNSDomain)) & vbTab & CBool( IsNull( objNetCard.DNSDomain))
  strResult = strResult _
    & vbNewLine & objNetCard.SetDNSDomain("mysuffix.com") 
''    strResult = strResult & vbNewLine & objNetCard.SetDNSSuffixSearchOrder("mysuffix.com") 

Next 
'strResult = strResult & vbNewLine

Wscript.Echo strResult

请参阅SetDNSDomain method return codes