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

How To Run Python Interactive Command Line Mode (CLI)

Windows PS C:\WINDOWS\system32> python Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit…

Resolving LDAPS Connection Errors

Errors: Can not connect to remote server: 5059 ERROR_CERTIFICATE_ERROR (unable to read server certificates from…

PowerShell: Elevating Credential

$jumpbox="127.0.0.1" <# # Static Credentials (unsecured) $username = (Get-ADDomain).name+"\ADMINISTRATOR" $password = "PASSWORD" #> # Dynamic…