VMware Cloud Community
nava_thulasi39
Jump to solution

VirtualSwitch and Virutalport group details

Hi,

I am trying to collect the information of each host vSwitch details and configured port group in the cluster.

I am fetching the result using the onliner like this.. (get-vmhost | get-virtualswitch | get-virtualportgroup)

But please some one assist me to get it as report like below

ClusterName  ESXhost vSwitch    Portgroups

X                    x1         vSwitch0   1, 2, 3, 4,5

                                  vSwitch1   6, 7, 8, 9, 10 

I am sure, this kind of query might have answered already. But i couldn't get it in the search.  Thanks in advance.

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I combined the virtualportgroup name and the VLanId, you'll get something like "pg1-123", where pg1 is the portgroupname and 123 is the VLanId.

foreach($cluster in Get-Cluster){
   
foreach($esx in Get-VMHost -Location $cluster){
       
Get-VirtualSwitch -VMHost $esx |
       
Select @{N="Cluster";E={$cluster.Name}},
           
@{N="ESXi";E={$esx.Name}},
           
@{N="vSwitch";E={$_.Name}},
           
@{N="Portgroups";E={[string]::Join(',',(Get-VirtualPortGroup -VirtualSwitch $_ | %{"$($_.Name)-$($_.VLanId)"}))}}
    }
}


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($cluster in Get-Cluster){
   
foreach($esx in Get-VMHost -Location $cluster){
       
Get-VirtualSwitch -VMHost $esx |
       
Select @{N="Cluster";E={$cluster.Name}},
           
@{N="ESXi";E={$esx.Name}},
           
@{N="vSwitch";E={$_.Name}},
           
@{N="Portgroups";E={[string]::Join(',',(Get-VirtualPortGroup -VirtualSwitch $_ | %{$_.Name}))}}
    }
}


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

nava_thulasi39
Jump to solution

Hi LucD,

Thanks for the quick reply.

I am getting the below error regarding the Join operator, Please help me to correct it. Without Join Opearator, it works fine.

Missing expression after ','.

At C:\Scripts\Temp\Portinfo.ps1:7 char:52

+             @{N="Portgroups";E={[string]::Join(',', <<<< Get-VirtualPortGroup

-VirtualSwitch $_ | %{$_.Name})}}

    + CategoryInfo          : ParserError: (,:String) [], ParseException

    + FullyQualifiedErrorId : MissingExpressionAfterToken

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There were some parenthesis missing, I updated the code.

Please try again.


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

0 Kudos
nava_thulasi39
Jump to solution

Thanks for the quick updated code.

Is there a chance to fetch the VLAN id from the Port groups? if yes, could you please update the code which includes VLAN id.

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I combined the virtualportgroup name and the VLanId, you'll get something like "pg1-123", where pg1 is the portgroupname and 123 is the VLanId.

foreach($cluster in Get-Cluster){
   
foreach($esx in Get-VMHost -Location $cluster){
       
Get-VirtualSwitch -VMHost $esx |
       
Select @{N="Cluster";E={$cluster.Name}},
           
@{N="ESXi";E={$esx.Name}},
           
@{N="vSwitch";E={$_.Name}},
           
@{N="Portgroups";E={[string]::Join(',',(Get-VirtualPortGroup -VirtualSwitch $_ | %{"$($_.Name)-$($_.VLanId)"}))}}
    }
}


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

0 Kudos
nava_thulasi39
Jump to solution

Hi LucD,

This is awesome.

Curious, what it does in $ here before ($_.Name) ***** {"$($_.Name)-$($_.VLanId)"}****, Is there any link to dig further?

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you reference a property of a variable in a string, you have to use this syntax to help the interpreter understand what you want.

A simple variable can be referenced as is in a string

$t = "ABC"

"My string $t"

But for a property you use the "special" syntax

$vm = Get-VM -Name MyVM

"VM name is $($vm.Name)"


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

0 Kudos