2015-11-07 12 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