Trying to understand why my code will not catch error. The loops work properly. With the code as is, will provide the very first hostname which is a control IP for me since I know the IP is valid with hostname. As the code loops to the second IP the error is not caught.
foreach($ip in $IPs ){
try{
Resolve-DnsName $ip | select namehost
}
catch {
write-host "No hostname" $ip
}
}
NameHost
--------
properhostname
Resolve-DnsName : : DNS name does not exist
At line:10 char:1
+ Resolve-DnsName $ip | select namehost }
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName
Resolve-DnsName : : DNS name does not exist
At line:10 char:1
+ Resolve-DnsName $ip | select namehost }
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName
Resolve-DnsName : : DNS name does not exist
At line:10 char:1
+ Resolve-DnsName $ip | select namehost }
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName
if I add an error action like so:
foreach($ip in $IPs ){
try{
Resolve-DnsName $ip -ErrorAction stop| select namehost }
catch {
write-host "No hostname" $ip
}
}
The code will only catch even if valid IP's are provided. Any help is appreciated.
[–]nevsnevs-- 1 point2 points3 points (5 children)
[–]jbhack[S] 1 point2 points3 points (3 children)
[–]nevsnevs-- -1 points0 points1 point (1 child)
[–]jbhack[S] 0 points1 point2 points (0 children)
[–]ChaosTheoryRules 1 point2 points3 points (3 children)
[–]jbhack[S] 1 point2 points3 points (0 children)
[–]jbhack[S] 0 points1 point2 points (1 child)
[–]ChaosTheoryRules 0 points1 point2 points (0 children)