I’m writing a very simple script to resolve DNS records with the Resolve-DnsName cmdlet. But I can’t get the error handling to work.
The error
Resolve-DnsName : HOSTNAME : DNS name does not exist
At line:6 char:17
- Resolve-DnsName $node -Type A | FT Name,IPAddress
-
-
CategoryInfo : ResourceUnavailable: (HOSTNAME:String) [Resolve-DnsName], Win32Exception
FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName
Details about the error
PS C:> $error[0] | fl * -Force
writeErrorStream : True
PSMessageDetails :
Exception : System.ComponentModel.Win32Exception (0x80004005): HOSTNAME : DNS name does not exist
at Microsoft.DnsClient.Commands.win32.GetDNSRecords(String QueryName, String DnsServerIPs, QueryOptions Options, RecordType Type)
at Microsoft.DnsClient.Commands.api.SendDnsQuery(Cmdlet HostCmdlet, String NameQueried, RecordType DNSQueryType, String DNSServerAddresses,
QueryParameters Switches)
TargetObject : HOSTNAME
CategoryInfo : ResourceUnavailable: (HOSTNAME:String) [Resolve-DnsName], Win32Exception
FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , : line 6
PipelineIterationInfo : {0, 1, 0}
The catch script block:
catch [System.ComponentModel.Win32Exception]
{
write-Warning “$node is not in DNS”
}
Running a normal catch without the exception class won’t work either.
Does anyone know a way around this?