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

Random Notes about WSUS

#Install the PowerShell Windows Update module $checkModule=Get-Module -ListAvailable -Name PSWindowsUpdate if(!($checkModule)){ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #…

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…

Indications that Chocolatey is locked down

# System's version is less than vendor's current (Chocolatey v0.10.15) 0+000+00[LAX-WEB005]: PS C:\Users\testadmin\Documents> choco source…