VMware Cloud Community
morphyno
Enthusiast
Enthusiast
Jump to solution

A faster way to retrieve all the VM's associated to a portgroup

I have a script that I use to add/remove portgroup to a large set of VM's. I'm curious if there's a faster to get the list of VM's associated to a port group.

Right now, a command like this

Get-VM |Get-NetworkAdapter | Where {$_.NetworkName -eq "PortGroupName" } 

Takes a long time as it is running for each VM.

When I log into VSphere, I see all the VM's under a portgroup right away, is there a better way through PowerCLI?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$pgName = 'PortgroupName'

$pg = Get-View -ViewType Network -Property Name,VM -Filter @{Name=$pgName}

Get-View -Id $pg.Vm -Property Name | Select-Object -ExpandProperty Name


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

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$pgName = 'PortgroupName'

$pg = Get-View -ViewType Network -Property Name,VM -Filter @{Name=$pgName}

Get-View -Id $pg.Vm -Property Name | Select-Object -ExpandProperty Name


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

HassanAlKak88
Expert
Expert
Jump to solution

Hello,

Print out a list of all the VMs and their associated network/portgroups:

Get-DataCenter DataCenterName | Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}},@{N="NIC";E={$_.Name}},@{N="Network";E={$_.NetworkName}}

To export the results to a CSV file run the following:

Get-DataCenter DataCenterName | Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}},@{N="NIC";E={$_.Name}},@{N="Network";E={$_.NetworkName}} | Export-csv C:\VMPortGroups.csv

Please consider marking this answer "correct" or "helpful" if you think your question have been answered correctly.

Cheers,

VCIX6-NV|VCP-NV|VCP-DC|

@KakHassan

linkedin.com/in/hassanalkak


If my reply was helpful, I kindly ask you to like it and mark it as a solution

Regards,
Hassan Alkak
Reply
0 Kudos
morphyno
Enthusiast
Enthusiast
Jump to solution

This is exactly what I was looking for. A way to find out which VM's belong to the PortGroup of interest. Thanks!

Reply
0 Kudos
HassanAlKak88
Expert
Expert
Jump to solution

Please consider marking this answer "correct" if you think your question have been answered correctly.


If my reply was helpful, I kindly ask you to like it and mark it as a solution

Regards,
Hassan Alkak
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think the user already did that yesterday :smileygrin:


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

firthmj2
Contributor
Contributor
Jump to solution

Thanks - this is very useful.

 

However, it does seem to have one weakness. If a second port group contains the string $pgName as well as one explicitly being called that, then the $pg seems to evaluate to all matching port groups. Is there a way to make the filter a "hard match" rather than a "sub string" as it seems to be at the moment?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The filter uses a RegEX, so you can use the RegEx anchors.
With the "start of string" or '^' and 'end of string' or '$' anchors the filter will look for an exact match of what is in $pgName

$pgName = 'PortgroupName'
$pg = Get-View -ViewType Network -Property Name,VM -Filter @{Name="^$pgName$"}
Get-View -Id $pg.Vm -Property Name | Select-Object -ExpandProperty Name

 


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

firthmj2
Contributor
Contributor
Jump to solution

Thank you, this is just what I needed!

Reply
0 Kudos