This took me a little while to get sorted as I had a gap in my PowerShell knowledge around the handling and formating of nested objects. A bit of exploration with get-member and I came up witha working script. It’s based on a post by Hugo Peeters and lists the VMs that are connected to (or belong to) a particular Port Group.
I wanted to know this as I was producing some infrastructure diagrams for a customer and wanted to know the names of VMs in a cluster for each Port Group without having to visit each ESX server.
The script is Get-VMPortGroups.ps1
Just call it and it will return a list of Port Groups and the VMs that belong to each. As an option, add a filter parameter to work with specific Port Groups. For example:
Get-VMPortGroups.ps1 “My Port Group”
Sorry if the script source is a bit long or clunky. I was having a bad day when I wrote it and made tonnes of silly mistakes.