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?
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
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
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|
This is exactly what I was looking for. A way to find out which VM's belong to the PortGroup of interest. Thanks!
Please consider marking this answer "correct" if you think your question have been answered correctly.
I think the user already did that yesterday :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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?
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
Thank you, this is just what I needed!