Posted On February 2, 2022

PowerShell: Get IP’s From Computer Names

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Get IP’s From Computer Names

Resolve from Names to IPs:

$names=@(
    'TESTVM001',
    'TESTVM002',
    'TESTVM003'
)
foreach($name in $names){
    $ips = [System.Net.Dns]::GetHostAddresses($name)
    write-host $ips
}

Resolve from IPs to Names:

$computerNames=@(
    'TESTVM001',
    'TESTVM002',
    'TESTVM003'
)
$regexIP = [regex] "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
foreach($computername in $computernames){
    $name = if($computername -match $regexIp){[System.Net.Dns]::GetHostByAddress($computername).hostname}else{$computername}
    write-host $name
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Some Tricks in Using the Here-String to Declare Blocks of Code

Trigger COMMAND CLI from within PowerShell # Fastest way to list all files in a…

Basic CSS: Add Rounded Corners with border-radius

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"><style>.red-text {color: red;}h2 {font-family: Lobster, monospace;}p {font-size: 16px;font-family: monospace;}.thick-green-border {border-color: green;border-width: 10px;border-style:…

PowerShell: How To Append to Windows Environmental Paths Permanently

Often, when we install applications such as Python, its bin directory may not automatically be…