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

miliarch commented Jan 30, 2020

Output Examples

Multiple VMs:

> Get-GuestInfo

liquid:
* Default Switch: 172.17.138.158, fe80::f844:69bb:cd0a:60bc
* Bridged: 192.168.1.30, fe80::d6c7:234e:745a:9bf9

Ubuntu 18.04.1 LTS:
* Default Switch: 172.17.138.152, fe80::6edd:4543:12dd:b57

Single VM:

> Get-GuestInfo ubuntu*

Ubuntu 18.04.1 LTS:
* Default Switch: 172.17.138.152, fe80::6edd:4543:12dd:b57

@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