Search code examples
powershellvirtual-machineporthyper-vmirroring

List all VM's by their port mirroring settings PowerShell


I'm looking for a way to list all VM's on a machine by the type of mirroring they have set using PowerShell.

For Example:

Get-VM -PortMirroring Source

And I'll see all VM's that have network adapters with Port Mirroring set to Source. But I know that Get-VM doesn't take PortMirroring as a parameter, so I'm wondering if there is a workaround?


Solution

  • This is not this easy, as PortMirroring is not the property of a VM, but rather a property of its network adapter. You need to iterate VMs' network adapters and output VMs that have at least 1 adapter that has PortMirroringMode set to "Source".

    $vms=get-vm
    $filteredVMs=@()
    foreach ($vm in $vms) { 
       $nas=get-vmnetworkadapter -vm $vm # adapter list
       foreach ($na in $nas) {
           if ($na.PortMirroringMode -eq 'Source') {
               $filteredVMs+=$vm
               break 
           }
       }
    }