0

I am trying to just get the IPv4 address from running Resolve-DnsName in PowerShell. I am not sure how to do this. This is my code so far but I am receiving errors. (Still brand new to PowerShell) Thanks!

Resolve-DnsName | Get-Process | Select-Object -Property IP4Address
2
  • 1
    Your command makes no sense. Resolve-DnsName takes a mandatory parameter (the name to resolve) and you can't pipe the output to Get-Process because that doesn't take DNS records but process names. What are you actually trying to accomplish? Get the IPv4 address of the local machine? An arbitrary machine? Getting a list of processes on that machine? May 1, 2020 at 18:49
  • 1
    To a first approximation, (Resolve-DnsName [machine]).IP4Address would do if all you want is the IPv4 address. This might return more than one address if there are multiple records, and is also not necessarily identical to the machine's idea of what the IP addresses of its network adapters are, which is why it's useful to know what you're really after. May 1, 2020 at 18:54

2 Answers 2

1

PowerShell commands return Objects which have a lot of properties. To start with, most cmdlets will give you their top four or five most relevant properties by default.

Resolve-DnsName citiesskylines.com 
Name                                           Type   TTL   Section    IPAddress                                
----                                           ----   ---   -------    ---------                                
citiesskylines.com                             A      60    Answer     34.235.38.236                            
citiesskylines.com                             A      60    Answer     52.72.29.26                              
citiesskylines.com                             A      60    Answer     54.209.87.84    

You can select any property by piping the output of this cmdlet into another called Select-Object and specifying the properties you want.

Resolve-DnsName citiesskylines.com | Select-Object -Property IPAddress

IPAddress    
---------    
34.235.38.236
52.72.29.26  
54.209.87.84 

You can store the output in a variable and use it later.

$IPAddresses = Resolve-DnsName citiesskylines.com | Select-Object -Property IPAddress

Then you can step through the results with a ForEach loop and do something with it.

ForEach($address in $IPAddresses){
    $thisIPAddress = $address.IPAddress
    Write-host "About to work with $thisIPAddress"
    #do something here
}

Which results in this

About to work with 34.235.38.236
About to work with 52.72.29.26
About to work with 54.209.87.84

From here, it's all basically working with lego pieces and figuring out which cmdlets you need to make something cool happen.

As a final tip, try piping any cmdlet into Format-List to see all of the other useful properties it might have, but not show by default!

0

If you know the hostname of the target machine, you can get an IPv4 address this way.

$hostname = <machine>
Resolve-DnsName $hostname | Where-Object {$_.IPAddress -like "*.*.*.*"}

If you know the subnet, you can replace any of the wild cards in the where-object as needed. As Jeroen said above, though, this can produce multiple different records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.