During a recent project we have been starting to use network segregation to give more security and control instead of using a flat network.
This has lead to VM’s being broken up in to there own VMware port groups and segregated VLAN’s.
There are now old port groups that have had all VM’s removed, so we wanted to report on any port groups that have no VM’s associated so that they can be removed as we will hit a issue with max VLAN limit per physical interface.
The quickest way I could think of to create the report was to use PowerCLI, in this post we will go through the process and commands used to create the report.
First we need to connect to vCenter using PowerCLI
We will be using a few different commands in the script.
First we will need to get list of port groups will only be getting distributed port groups as we don’t use standard port groups.
Get-VDPortgroup

Second part of the script is to get the port group view using get-view
Get-View -ViewType Network -Property Name -Filter @{"Name" = "portgroup name"}

We will be using UpdateViewData to add some addtional values to the view to make the script quicker and easier to read see below link for more details on UpdateViewData.
https://blogs.vmware.com/PowerCLI/2011/08/optimize-the-performance-of-powerclis-views.html
To find the properties we wanted to report on we used
Get-VM -Name VMName | Get-View

Use Get-VIObjectByVIView to get the host information.
$vm = Get-VM -name VMName | get-view
(Get-VIObjectByVIView $vm.runtime.host).name

I want to report on the VM name, Host, Cluster and PowerState.
Below we will get the view for the port group and update the view with the additional details that I want in the report.
$networks = Get-View -ViewType Network -Property Name -Filter @{"Name" = "portgroup name"}
$networks | ForEach-Object{($_.UpdateViewData("Vm.Name","Vm.Runtime.Host.Name","Vm.Runtime.Host.Parent.Name","vm.Runtime.PowerState"))}

We can run the below to check if the addtional information has been added to the veiw
$networks.LinkedView.vm.name

Once we have the view working and adding the properties we want we can start to create the full script.
The full script can be copied from the below github link.
https://github.com/TheSleepyAdmin/Scripts/blob/master/VMware/Network/VMware_PortGroupReport.ps1
There are two parameters in the script.
To just output to the console screen use -ConsoleOnly parameter.

To export to csv use the -ReportExport parameter.

Below is a example of the exported csv.

You saved my script! THANK YOU
LikeLike