VMware Cloud Community
vmCalgary
Enthusiast
Enthusiast

List virtualportgroups in a cluster count VMs and VMhosts

Get-Datacenter "CG-SO" | Get-Cluster|Get-VMHost |Get-VirtualPortGroup tells me the virtualportgroups and vlans.

NPR--10.1.202.0_24        key-vim.host.PortGroup-NPR-... 202

NPR--10.1.201.0_24        key-vim.host.PortGroup-NPR-... 201

NPR--10.1.113.0_24        key-vim.host.PortGroup-NPR-... 113

NPR--10.1.106.0_24        key-vim.host.PortGroup-NPR-... 106

I can get host and vm counts with the following:

Get-Cluster | Select Name, @{N="Host Count"; E={($_ | Get-VMHost).Count}},  @{N="VM Count"; E={($_ | Get-VM).Count}}

How do I combine the two to get the following?

Cluster                                         VirtualPortGroup          Vlan   VMhost Count   VM Count

CG-SO-PRE-DMZ-WINDOWS  NPR--10.1.106.0_24      106                  3                   50

I want to identify those virtual port groups no longer in use to evaluate if they need to be removed. We've had several vlans removed from our network infrastructure over the years.

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

Try something like this.

Get-Datacenter -PipelineVariable dc |

Get-Cluster -PipelineVariable cluster |

Get-VMHost |

Get-VirtualPortGroup |

  Select @{N = 'Datacenter'; E = {$dc.Name}},

   @{N = 'Cluster'; E = {$cluster.Name}},

  Name, VlanId,

   @{N = 'VMCount'; E = {($_ | Get-VM).Count}} |

Group-Object -Property Datacenter, Cluster, Name |

ForEach-Object -Process {

   $_ | Select @{N = 'Datacenter'; E = {$_.Name.Split(',')[0].Trim(' ')}},

   @{N = 'Cluster'; E = {$_.Name.Split(',')[1].Trim(' ')}},

   @{N = 'Portgroup'; E = {$_.Name.Split(',')[2].Trim(' ')}},

   @{N = 'VlanId'; E = {$_.Group[0].VlanId}},

   @{N = 'VMHostCount'; E = {$_.Count}},

   @{N = 'VMCount'; E = {($_.Group | Measure-Object -Property VMCount -Sum).Sum}}

}


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

Reply
0 Kudos
vmCalgary
Enthusiast
Enthusiast

This appears 6 times and then I get exactly what I want for the first datacenter (CG-CO-SBODEVDC). I have to assume that the errors I'm getting is one error per datacenter.

Get-Cluster : 2/11/2019 4:06:43 PM      Get-Cluster             VMHost parameter: Could not find any of the objects specified by name.

At line:2 char:1

+ Get-Cluster -PipelineVariable cluster

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...VMHost[] VMHost:RuntimePropertyInfo) [Get-Cluster], ObnRecordProcessingFailedException

    + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetCluster

    

DatacenterClusterPortgroupVlanIdVMHostCountVMCount
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBManagement Network250040
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBvMotionB251140
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBvMotionA251140
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBNFS_172.28.4300440
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBStorageB251040
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBStorageA251040
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBNPR--10.15.80.0_24208040
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBNPR--10.15.190.0_23319040
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBNPR--10.15.182.0_24318241
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBSBO--10.15.184.0/22--NPR31844176
CG-CO-SBODEVDCCG-CO-SBODEV-ORADBNFS_Direct_172.28.5630564176

> Get-Datacenter -PipelineVariable dc

Name

----

CG-CO-SBODEVDC

CG-SO

CG-ZZ

CG-CL

CG-NO

CG-NL

CG-CO

Reply
0 Kudos
LucD
Leadership
Leadership

It looks as if the pipeline symbol at the end of the Get-Cluster line was dropped.

Can you attach the code as you are running it?


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

Reply
0 Kudos
vmCalgary
Enthusiast
Enthusiast

Luc,

I have attached the script I used.

1. I'd like to isolate it to only look at the datacenter  "CG-SO" and not my entire environment as well.

2. Export to csv.

3. Pipeline symbol was not dropped (dang, that would have been an easy fix).

Get-Datacenter -PipelineVariable dc |

Get-Cluster -PipelineVariable cluster |

Get-VMHost |

Get-VirtualPortGroup |

  Select @{N = 'Datacenter'; E = {$dc.Name}},

   @{N = 'Cluster'; E = {$cluster.Name}},

  Name, VlanId,

   @{N = 'VMCount'; E = {($_ | Get-VM).Count}} |

Group-Object -Property Datacenter, Cluster, Name |

ForEach-Object -Process {

   $_ | Select @{N = 'Datacenter'; E = {$_.Name.Split(',')[0].Trim(' ')}},

   @{N = 'Cluster'; E = {$_.Name.Split(',')[1].Trim(' ')}},

   @{N = 'Portgroup'; E = {$_.Name.Split(',')[2].Trim(' ')}},

   @{N = 'VlanId'; E = {$_.Group[0].VlanId}},

   @{N = 'VMHostCount'; E = {$_.Count}},

   @{N = 'VMCount'; E = {($_.Group | Measure-Object -Property VMCount -Sum).Sum}}

}

Reply
0 Kudos
LucD
Leadership
Leadership

Strange error in that case.ith this version.

Try with version

Get-Datacenter -Name CG-SO |

   Get-Cluster -PipelineVariable cluster|

   Get-VMHost |

   Get-VirtualPortGroup |

  Select @{N = 'Datacenter'; E = {$dc.Name}},

@{N = 'Cluster'; E = {$cluster.Name}},

Name, VlanId,

@{N = 'VMCount'; E = {($_ | Get-VM).Count}} |

   Group-Object -Property Datacenter, Cluster, Name |

   ForEach-Object -Process {

   $_ | Select @{N = 'Datacenter'; E = {$_.Name.Split(',')[0].Trim(' ')}},

   @{N = 'Cluster'; E = {$_.Name.Split(',')[1].Trim(' ')}},

   @{N = 'Portgroup'; E = {$_.Name.Split(',')[2].Trim(' ')}},

   @{N = 'VlanId'; E = {$_.Group[0].VlanId}},

   @{N = 'VMHostCount'; E = {$_.Count}},

   @{N = 'VMCount'; E = {($_.Group | Measure-Object -Property VMCount -Sum).Sum}}

} |

   Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
vmCalgary
Enthusiast
Enthusiast

LucD I get the same error. I'm getting a peer to run to see if it's my environment or version of powercli.

Reply
0 Kudos
LucD
Leadership
Leadership

Are you sure you have the required rights on all resources in that datacenter?

Could it be that you have a cluster without ESXi nodes?


Which PowerCli version are you using?

And from which platform (OS + PS version)?


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

Reply
0 Kudos