一个问题难倒了这里 - 运行PowerShell的1.0PowerShell的错误 - GetHostByAddress
代码(假设IP地址是在被传递有效):
$ips = @(Import-CSV $attachmentLocation)
foreach ($ip in $ips){
$ipAddress = $ip.IPAddress
$length = $ipaddress.length
write-host "Length is: ($length)"
if(Test-Connection -ComputerName $ipAddress -Count 1 -ea silentlycontinue) {
write-host $ipAddress
$hostData = ([Net.Dns]::GetHostByAddress($ipAddress)).HostName
}
}
输出:
Length is: (11)
10.xx.xx.xx
Exception calling "GetHostByAddress" with "1" argument(s): "The requested name is valid, but no data of the requested type was found"
At FileName:13 char:43
+ $hostData = ([Net.Dns]::GetHostByAddress <<<< ($ipAddress)).HostName
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
如果我运行以下它工作正常 - 似乎是数据类型问题(im传递字符串值):
$hostData = ([Net.Dns]::GetHostByAddress("10.xx.xx.xx")).HostName
工作代码:
$ipAddress = "10.xx.xx.xx"
$hostData = ([Net.Dns]::GetHostByAddress($ipAddress)).HostName
答: 问题是与ActiveDirectory域名DNS解析不是命令,而一些IP地址是ping命令他们不要在那里运行脚本的机器上妥善解决。这导致错误'找不到所请求类型的数据'是指它无法将IP解析为DNS名称。
$ ipaddress从哪里来?任何机会都有尾随空白?你在那个地址有RDNS吗? – mjolinor
没有结尾的空格,当我直接以字符串的形式输入IP地址时工作正常。已经搜索了互联网已经没有成功。谢谢回复! –
什么意思是“该域名存在问题”?你能否进一步解释? – Suncatcher