I am trying to get the ipaddress from a hostname using Powershell, but I really can't figure out how.
Any help?
In an open command line, type ping followed by the hostname (for example, ping dotcom-monitor.com). and press Enter. The command line will show the IP address of the requested web resource in the response. An alternative way to call Command Prompt is the keyboard shortcut Win + R.
Type cast to [System.PowerShell Type accelerator [IPAddress] which is also an alias of System. Net. IPAddress class can validate IP Addresses if it is typecast-ed against it, and will throw an error when IP Address is out of range or is incorrect in format.
Just add the -Name parameter to the Resolve-DnsName cmdlet and pass it a computer name. It will search the locally assigned DNS server for a record with the matching computer name and return the record information.
You can get all the IP addresses with GetHostAddresses like this:
$ips = [System.Net.Dns]::GetHostAddresses("yourhosthere")
You can iterate over them like so:
[System.Net.Dns]::GetHostAddresses("yourhosthere") | foreach {echo $_.IPAddressToString }
A server may have more than one IP, so this will return an array of IPs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With