VMware Cloud Community
KMHH
Contributor
Contributor
Jump to solution

Distributed Port Group - query for connected VMs in PowerShell

Hi folks,

I have a distributed port group in VMware that I want to query for connected VMs.

The code below returns VMs; but the names are not what I was expecting.

$portgroups = Get-VDPortgroup -Name vLAN-1234

foreach ($pg in $portgroups) {

     $pg.ExtensionData.vm

}

Type           Value  

Type                   Value

----                      ----- 

VirtualMachine   vm-4679

VirtualMachine   vm-2414

VirtualMachine   vm-8691

How do track this "VM name" back to a VM?

Thanks in advance. Smiley Happy

Reply
0 Kudos
1 Solution

Accepted Solutions
KMHH
Contributor
Contributor
Jump to solution

Updated PowerShell code.

$portgroups = Get-VDPortgroup -Name vLAN-1234
$connectedvms = @()
foreach ($pg in $portgroups) {
    $vms = $pg.ExtensionData.vm
    foreach ($vm in $vms) {
        $vmid           = "virtualmachine-" + $vm.value
        $connectedvm    = $null
        $connectedvm    = (Get-View -Id $vmid).name
        $connectedvms  += $connectedvm
    }
}
$connectedvms | sort

Came up with the answer after posting!

Might be of use to someone somewhere.

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.

View solution in original post

Reply
0 Kudos
1 Reply
KMHH
Contributor
Contributor
Jump to solution

Updated PowerShell code.

$portgroups = Get-VDPortgroup -Name vLAN-1234
$connectedvms = @()
foreach ($pg in $portgroups) {
    $vms = $pg.ExtensionData.vm
    foreach ($vm in $vms) {
        $vmid           = "virtualmachine-" + $vm.value
        $connectedvm    = $null
        $connectedvm    = (Get-View -Id $vmid).name
        $connectedvms  += $connectedvm
    }
}
$connectedvms | sort

Came up with the answer after posting!

Might be of use to someone somewhere.

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.

Reply
0 Kudos