I am looking for some help gathering a list of portgroups and vlans by cluster & vmhost.
Desired column headers:
Cluster
VMhost
Portgroup
vlan
vcenter
Counts of VMs and VMHosts per cluster is a 'would be nice' to have as well.
Sorry for the extra bracket in the second line. Below is my suggestion:
$vcConnection = Connect-VIServer -Server <server_IP? -User <user_name> -Password <user_password>
$dc = Get-Datacenter -Server $vcConnection
$objects = $dc | Get-VMHost -Server $vcConnection| Get-VirtualPortGroup -Server $vcConnection
foreach($obj in $objects) {
$clusterResult = $dc | Get-Cluster -Server $vcConnection
foreach ($cluster in $clusterResult) {
$vmHost = $cluster | Get-VMHost
$vmHost | Get-VirtualPortGroup | ? {$_.Name -eq $obj.Name} | select @{N='Datacenter'; E = {$dc.Name}}, @{N = 'Portgroup'; E = {$obj.Name}}, @{N = 'VLanId'; E = {$obj.VLanId}}, @{N = 'ClusterName'; E = {$cluster.Name}}, @{N = 'VCenter' ; E={$vcConnection}}, @{N = 'VMHostCount'; E = {$vmHost.Count}}, @{N = 'VMCount'; E = {($vmHost|Get-VM).Count}}
}
}
What do you already have?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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}}
}
I have made some modifications of Luc's script:
$vcConnection = Connect-VIServer -Server <server_IP? -User <user_name> -Password <user_password>
$dc = (Get-Datacenter -Server $vcConnection
$objects = $dc | Get-VMHost -Server $vcConnection| Get-VirtualPortGroup -Server $vcConnection
foreach($obj in $objects) {
$dc | Get-VMHost | Get-VirtualPortGroup | ? {$_.Name -eq $obj.Name} | select @{N='Datacenter';'E' = {$dc.Name}},@{N = 'Portgroup'; E = {$obj.Name}}, @{N = 'VLanId'; E = {$obj.VLanId}}
}
Does this work for you ?
Not entirely. The second line was missing the closing bracket and the cluster information was missing. This is to be used as part of a lifecycle replacement project.
Sorry for the extra bracket in the second line. Below is my suggestion:
$vcConnection = Connect-VIServer -Server <server_IP? -User <user_name> -Password <user_password>
$dc = Get-Datacenter -Server $vcConnection
$objects = $dc | Get-VMHost -Server $vcConnection| Get-VirtualPortGroup -Server $vcConnection
foreach($obj in $objects) {
$clusterResult = $dc | Get-Cluster -Server $vcConnection
foreach ($cluster in $clusterResult) {
$vmHost = $cluster | Get-VMHost
$vmHost | Get-VirtualPortGroup | ? {$_.Name -eq $obj.Name} | select @{N='Datacenter'; E = {$dc.Name}}, @{N = 'Portgroup'; E = {$obj.Name}}, @{N = 'VLanId'; E = {$obj.VLanId}}, @{N = 'ClusterName'; E = {$cluster.Name}}, @{N = 'VCenter' ; E={$vcConnection}}, @{N = 'VMHostCount'; E = {$vmHost.Count}}, @{N = 'VMCount'; E = {($vmHost|Get-VM).Count}}
}
}
Perfection. Thanks for your help.