I'm trying to get all the errors to a separate file so that we can clean up the DNS srv later.
What I do want is to get the error message:
+ CategoryInfo : ResourceUnavailable: (bonjour.Domainname.com:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName
Resolve-DnsName : Domainname.com : DNS-namnet does not exist
to end up in a separate file, is this possible?
I'm using this script at the moment:
$path = ".\domain.txt"
$dnsserver = @('DNS-Srv1', 'DNS-Srv2' , 'DNS-Srv3')
$mxdomain = Get-Content $path
foreach ($domain in $mxdomain) {
Resolve-DnsName -Name $domain -Type MX -Server $dnsserver | select Name,Type,NameExchange } ```
You can use Try Catch
to handle the exception. Within a Catch block, the current error can be accessed using $_ and the object will of of type ErrorRecord
. It has a ToString
method which you can use to get the string representation of your error record.
try { Resolve-DnsName -Name www.bing122222.com -ErrorAction Stop } catch { $error_message = $_.ToString() # this will print 'www.bing122222.com : DNS name does not exist' Write-Host $error_message }