powershellnslookup

Powershell Resolve-DnsName to variable


I'm trying to put data of Resolve-DnsName mmydomain.com to a variable.

$data = Resolve-DnsName mmydomain.com 
Write-host $data

As a result of this scrip, I can see only this string Microsoft.DnsClient.Commands.DnsRecord_A, while simple execution of Resolve-DnsName mmydomain.com writes me full nslookup data.

Any advice, please?


Solution

  • The IP information is in $data.IPAddress - the below lists ALL the properties that are retrieved when you run the Resolve-DnsName cmdlet:

    $data = Resolve-DnsName mmydomain.com 
    $data | Select *
    

    If you want one(or more) of the properties, ask for it(them) by name:

    $data = Resolve-DnsName mmydomain.com 
    # By Select method
    Write-Host 'Select the IpAddress property of $data:'
    $data | Select IPAddress | Out-Host
    # By property name
    Write-Host 'Get by property name:'
    $data.IpAddress
    

    Generally you would just use what you need e.g.

    $Data | select Name,Ip4Address,Type
    

    So in your example:

    If ($data.IpAddress -like "*11.22.33.44*") {
        Write-Host "Got it!"
    }
    Else {
        Write-Host "Nothing found!"
    }