我想在powershell脚本中运行nslookup,将输出分配给我可以解析的字符串变量 . 我不希望在来自CMD执行的powershell窗口中看到类似“非权威性答案:”的回声,但我试图将命令的输出专门管道或重定向到变量的所有内容都没有工作或破坏变量 .
例:
PS> $temp = (& nslookup 'myip.opendns.com','resolver1.opendns.com');
Non-authoritative answer:
我尝试了几种解决方法......
PS> $temp = Invoke-Expression "cmd /c nslookup myip.opendns.com resolver1.opendns.com" >$null
PS> $temp = Invoke-Expression "cmd /c @ECHO off && nslookup myip.opendns.com resolver1.opendns.com"
也许有更好的方法来做到这一点 . 我在这里使用字符串来获取IP地址的方式比我想要的多几行 .
$temp = (nslookup myip.opendns.com resolver1.opendns.com) | Out-String
$temp = $temp.split()
$tempIPs = $temp -match "^(.|\r|\n)*?\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b"
$publicIP = $tempIPs[1]
return $PublicIP
我在使用nslookup,不是't seem to work with powershell' s Resolve-DnsName命令 . 我需要两台服务器,因为我正在重定向查找以获取我的公共IP . 还有其他方法可以做到这一点,但是这个方法非常有效,并且在处理这个脚本时_1116126_已经让我失望了 .
1 回答
您可以通过指定
Resolve-DnsName
的-Server
参数来指定要解决查询的服务器 .从MSDN documentation为
Resolve-DnsName
命令: