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: Get Event Logs from a List of Computers

Windows event logs contain a wealth of information that would be useful for analytical purposes.…

PowerShell: Unjoin Computer From Domain

# unjoinComputerFromDomain.ps1 # Version 0.02 # Notes: # - This function doesn't delete the referenced…

Basic HTML and HTML5: Create a Form Element

<h2>CatPhotoApp</h2><main><p>Click here to view more <a href="#">cat photos</a>.</p><a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying…