Powershell Resolve-DnsName 到变量

Powershell Resolve-DnsName to variable

我正在尝试将 Resolve-DnsName mmydomain.com 的数据放入变量。

$data = Resolve-DnsName mmydomain.com 
Write-host $data

作为此脚本的结果,我只能看到此字符串 Microsoft.DnsClient.Commands.DnsRecord_A,而 Resolve-DnsName mmydomain.com 的简单执行会向我写入完整的 nslookup 数据。

有什么建议吗?

IP 信息位于 $data.IPAddress - 下面列出了当您 运行 Resolve-DnsName cmdlet:

时检索到的所有属性
$data = Resolve-DnsName mmydomain.com 
$data | Select *

如果您想要一个(或多个)属性,请按名称索取(它们):

$data = Resolve-DnsName mmydomain.com 
# By Select method
Write-Host 'Select the IpAddress property of $data:'
$data | Select IPAddress | Out-Host
# By property name
Write-Host 'Get by property name:'
$data.IpAddress

通常你会使用你需要的东西,例如

$Data | select Name,Ip4Address,Type

所以在你的例子中:

If ($data.IpAddress -like "*11.22.33.44*") {
    Write-Host "Got it!"
}
Else {
    Write-Host "Nothing found!"
}