Skip to content

Instantly share code, notes, and snippets.

@miliarch
Created January 30, 2020 09:01
Show Gist options
  • Save miliarch/c15fbc1bc71fe670da6ae5b8214c2e0a to your computer and use it in GitHub Desktop.
Save miliarch/c15fbc1bc71fe670da6ae5b8214c2e0a to your computer and use it in GitHub Desktop.
PowerShell function to list IP address detail for running Hyper-V VMs
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
}
@miliarch
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment