Home > front end >  Resolve-DNSName get errors to seperate file
Resolve-DNSName get errors to seperate file

Time:09-21

Hei, I'm trying to get all the errors to a seperate file so that we can clean up up DNS srv later.

I'm using this script at the moment: 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 seperate file, is this possible?

$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 } ```

CodePudding user response:

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 
}
  • Related