VMware Cloud Community
lwyplalk
Contributor
Contributor

Listing all the virtual machines that are connected to the distributed portgroup

Hello VMware Community!

I am experiencing some difficulties in listing all the virtual machines that are connected to the distributed portgroup called vDS-10 within a cluster named Cluster1. While using Get-VirtualPortGroup command is easy, it seems that retrieving the VM list from specific vds port groups is impossible.

Has anyone else experienced a similar issue and found a way to solve it?

 

 

0 Kudos
10 Replies
LucD
Leadership
Leadership

Did you try with

Get-VDPortgroup -Name vDS-10 | Get-VM -Location (Get-Cluster -Name Cluster1)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

BarryGrowler
Enthusiast
Enthusiast

To list VMs connected to the distributed port group "vDS-10" in the cluster "Cluster1", use this PowerCLI command:

Get-VDPortgroup -Name "vDS-10" | Get-VM -Location (Get-Cluster -Name "Cluster1")

You can extend the command to retrieve additional VM details, like:

Get-VDPortgroup -Name "vDS-10" | Get-VM -Location (Get-Cluster -Name "Cluster1") | Select-Object Name, PowerState, NumCpu, MemoryGB

LucD
Leadership
Leadership

Really? You just repeat my reply 😄


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

lwyplalk
Contributor
Contributor

Thank you all for all messages. Any chance to list name names from specific vLAN tag on vDS?

 

Get-VM : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input
or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:40
+ ... -Name *-10 | Get-VM -Location (Get-Cluster -Name CL05)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (***-10:PSObject) [Get-VM], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

0 Kudos
lwyplalk
Contributor
Contributor

I've tried to list all VMs from specific cluster and VLANs, ie. 10, 44, 55 on cluster CL01. No luck.

Get-VDPortgroup : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match
any of the parameters that take pipeline input.

 

Get-Cluster CL01 | Get-VM | where { ($_ | Get-VDPortgroup | where {$_.vlanid -match "10"})} | Select Name,
@{N="MacAddress";E={[string]::Join(',',($_.Guest.Nics | %{$_.MacAddress}))}},
@{N="VLanID";E={[string]::Join('#',(Get-VDPortgroup -VM $_ | %{$_.VLanID}))}}

0 Kudos
LucD
Leadership
Leadership

The Get-VDPortgroup cmdlet doesn't accept a VirtualMachine object in the pipeline.

Try something like this

Get-Cluster CL01 |
Get-VM -PipelineVariable vm |
Get-NetworkAdapter -PipelineVariable vnic |
Get-VDPortgroup -PipelineVariable pg | Where-Object { $_.VlanConfiguration.VlanId -in 10,44,55 } |
ForEach-Object -Process {
  New-Object -TypeName PSObject -Property ([ordered]@{
    Name = $vm.Name
    VNic = $vnic.Name
    MacAddress = $vnic.MacAddress
    VLanID = $pg.VlanConfiguration.VLanID
  })
}





Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

lwyplalk
Contributor
Contributor

this is odd. script is hanging there thinking and finally, I'm getting output but it's not complete. I tried on 3 vCenters. I get just 1 record which is correct though. Reducing MacAddress and VLanID speeds up the execution but ofc the same output. Weird. 

Edit:

I wonder if there is another way to list all machines from a VLAN, rather than checking machines one by one, network adapter, vlan, select and then output. 
On Center its easy - portgroup(dvs), VMs tabs view.

Bot of course thank you @LucD for your effort in helping! KUDOS! 

0 Kudos
LucD
Leadership
Leadership

Out of curiosity, can you give the following a try?
Should, in theory, be faster

 

$clusterName = 'CL01'

$cluster = Get-View -Property Name -ViewType ClusterComputeResource -Filter @{Name = "^$($clusterName)$"}
$vms = Get-View -ViewType VirtualMachine -SearchRoot $cluster.MoRef -Property Name

Get-View -ViewType DistributedVirtualPortgroup -Property Name, VM, 'Config.DefaultPortConfig' -PipelineVariable pg |
where{$_.VM} |
ForEach-Object -Process {
  Get-View -Id $pg.VM -Property Name, 'Config.Hardware.Device' -PipelineVariable vm |
  where{$_.MoRef -in $vms.MoRef} |
  ForEach-Object -Process {
    $vm.Config.Hardware.Device.Where{ $_ -is [VMware.Vim.VirtualEthernetCard] -and $_.Backing -is [VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo] } |
    ForEach-Object -Process {
      New-Object -TypeName PSObject -Property ([ordered]@{
          VM = $vm.Name
          vNIC = $_.DeviceInfo.Label
          MacAddress = $_.MacAddress
          vLANId = $pg.Config.DefaultPortConfig.Vlan.VlanId
        })
    }
  }
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lwyplalk
Contributor
Contributor

It just list machines in the below format. Example: 

Name                 : VM1

Capability           :

Config               : VMware.Vim.VirtualMachineConfigInfo

Layout               :

LayoutEx             :

Storage              :

EnvironmentBrowser   :

ResourcePool         :

ParentVApp           :

ResourceConfig       :

Runtime              :

Guest                :

Summary              :

Datastore            :

Network              :

Snapshot             :

RootSnapshot         :

GuestHeartbeatStatus : gray

LinkedView           :

Parent               :

CustomValue          :

OverallStatus        : gray

ConfigStatus         : gray

ConfigIssue          :

EffectiveRole        :

Permission           :

DisabledMethod       :

RecentTask           :

DeclaredAlarmState   :

TriggeredAlarmState  :

AlarmActionsEnabled  : False

Tag                  :

Value                :

AvailableField       :

MoRef                : VirtualMachine-vm-4441

Client               : VMware.Vim.VimClientImpl

0 Kudos
LucD
Leadership
Leadership

Oops, typo.
I forget a pipeline symbol on this line

where{$_.MoRef -in $vms.MoRef} |

Code above is corrected, please try again.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos