Created
January 30, 2020 09:01
-
-
Save miliarch/c15fbc1bc71fe670da6ae5b8214c2e0a to your computer and use it in GitHub Desktop.
PowerShell function to list IP address detail for running Hyper-V VMs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-GuestInfo($VMName) { | |
$VMs = @() | |
if ($VMName) { | |
$VMs += Get-VM $VMName | Where-Object State -eq "Running" | |
} else { | |
$VMs += Get-VM | Where-Object State -eq "Running" | |
} | |
ForEach ($VM in $VMs) { | |
$NICs = $VM | Select-Object -ExpandProperty NetworkAdapters | |
if ($NICs) { | |
Write-Host | |
Write-Host "$($VM.Name):" | |
ForEach ($NIC in $NICs) { | |
$NICDetail = New-Object psobject -Property @{ | |
SwitchName = $NIC.SwitchName | |
IPAddresses = $NIC.IPAddresses -Join ", " | |
} | |
Write-Host "* $($NICDetail.SwitchName): $($NICDetail.IPAddresses)" | |
} | |
} | |
} | |
Write-Host | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: To use Get-VM, your local user account needs to belong to the "Hyper-V Administrators" group or you'll get a permission error. This change is easily made in Computer Management > Local Users and Groups, and requires logging off and on before membership is recognized.