一個問題難倒了這裏 - 運行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