Search code examples
powershellscriptingwindows-server-2012-r2

Get-VM command is not always returning Notes for some reason?


I'm trying to write a simple PS script to get the list of VM's on a Hyper-V host and for some reason Notes are not always returned when -ExpandProperty NetworkAdapters is used. See Screenshot:

See screenshot here.

Here are two different commands I'm using:

Get-VM | Select-Object -ExpandProperty NetworkAdapters | Select-Object VMName,Status, IPAddresses,ComputerName, Hostname, Notes | Format-Table -AutoSize
Get-VM | Select-Object VMName,IPAddresses,ComputerName, Hostname, Notes | Format-Table -AutoSize

I'm trying to get:

  • VM Name (VMName)
  • VM's Status (Whether it's running/stopped. Status seems to work ok)
  • The IPv4 address (I'm using IPAddresses)
  • The VM's hostname (ComputerName and Hostname aren't currently working, I'll figure this out later)
  • Notes on the vm (Notes doesn't always return a value)

So any idea why Notes do not always return?


Solution

  • If you try and pipe Get-VM | Select-Object -ExpandProperty NetworkAdapters to get-member it will not return the Notes Property. And thats why you can´t get it to display the notes.

    You could instead make a pscustomobjet, maybe something like this.

    $vms = Get-VM
    
    ForEach ($vm in $vms) {
        [PSCustomObject][ordered]@{
            'VMName'                   = $vm.Name
            'VM Status '               = $vm.Status
            'IP addresse'              = ($vm | Select-Object -ExpandProperty NetworkAdapters).ipaddresses
            'Bytes Total/sec Ethernet' = $vm.computername
            'Bytes Total/sec WiFi'     = $vm.notes
        }    
    }
    

    /Anders