2015-11-07 16 views
2

我想做一个Windows 10批处理文件,从2个选项交换我的DNS。第一个是8.8.8.8(google dns),第二个是允许我观看美国netflix的自定义DNS(如90.90.90.90)。批处理文件,改变2ns之间的选项

bash命令来更改DNS是

netsh interface ipv4 add dnsserver "Ethernet" address=8.8.8.8 index=1 

的命令查看DNS是否运行起来:

nslookup 

现在我想要做的IF THEN ELSE这样的工作:

if (dns == 8.8.8.8) 

then (change them to 90.90.90.90) 

else (change them to 8.8.8.8) 

即使是PowerShell脚本是好的

+0

[5用于在Windows中更改DNS服务器的实用程序](http://helpdeskgeek.com/free-tools-review/5-utilities-changing-dns-servers-windows-reviewed/) – Cyrus

+0

另请参阅http:// blogs.techne t.com/b/heyscriptingguy/archive/2009/02/26/how-do-i-query-and-retrieve-dns-information.aspx – lit

+0

我曾经使用一个运行在任务托盘中的小程序,该程序已经预定义网络设置我可以选择。我只是不记得该程序的名称。我用了很多,因为我在农村学区工作,我在每所学校使用了不同的静态IP地址。如果我再次找到它,我会再回复。但我相信你也可以通过Google搜索。 – Squashman

回答

0

基本上是:

 # Setup the dos command process information: 
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo 
    $pinfo.FileName = "nslookup" 
    $pinfo.Arguments = "" 
    $pinfo.UseShellExecute = $false 
    $pinfo.CreateNoWindow = $true 
    $pinfo.RedirectStandardOutput = $true 
    $pinfo.RedirectStandardError = $true 

    # Start the process: 
    $process.Start() | Out-Null 

    # Wait a while for the process to do something: 
    sleep -Seconds 5 

    # If the process is still active kill it: 
    if (!$process.HasExited) { 
     $process.Kill() 
    } 

    # get output from stdout and stderr: 
    $stdout = $process.StandardOutput.ReadToEnd() 
    $stderr = $process.StandardError.ReadToEnd() 

    # Parse the IP address from the output: 
    $regex = [regex] "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" 
    $regex.Matches($stdout) | %{ $Condition = $_.value } 

    # Do your dns change based on the output: 
    if ($Condition -eq "8.8.8.8") 
    { 
     "set other dns" 
    } 
    Else 
    { 
     "set google dns" 
    } 

抱歉,我不能成熟的答案多一点;意外的访客。

- 编辑:

考虑到一些评论,并与我的正则表达式中的错误,这可能是一个更合适的解决方案:

[cmdletbinding()]    
param ([parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName = $env:computername)       

begin {}    
    process {    
     # get current DNS setting: 
     foreach ($Computer in $ComputerName) {    
      if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) 
      {    
      try {    
       $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop | ? {$_.IPEnabled}    
      } 
      catch 
      {    
       Write-Warning "Error occurred while querying $computer."    
       Continue    
      }    
      foreach ($Network in $Networks) 
      {    
       $Condition = $Network.DNSServerSearchOrder; 
       break; 
      }    
      }    
     }    
     # Do your dns change based on the output: 
     if ($Condition -eq "8.8.8.8") 
     { 
      "set other dns"; 
     } 
     Else 
     { 
      "set google dns"; 
     } 
    }    

end {} 
+0

为什么在使用WMI或Get-DnsClientServerAddress cmdlet(Windows 8.1和anove)轻松处理对象时解析文本? – bluuf

+0

因为Matteo表示他正在使用nslookup命令;但更重要的是因为基本模式可以重新用于一些需要一些if/then/else逻辑的管理任务。 – Paul

+0

这是一个PowerShell脚本?因为错误列表太长了,如果我尝试在PS上运行它,我不明白它是否只是“指南”或实际脚本。 当然,我改变了if/then/else部分。 –

1

这应该工作:

@echo off 

set "dns=8.8.8.8" 
for /F "skip=1 tokens=2" %%a in ('nslookup^<NUL') do if "%%a" equ "8.8.8.8" set "dns=90.90.90.90" 
netsh interface ipv4 add dnsserver "Ethernet" address=%dns% index=1