VMware Cloud Community
esloof
Expert
Expert
Jump to solution

Get PortGroup List

I'm trying to build a script that extracts all PortGroups with a certain VLAN ID. The only thing that's missing is the output Smiley Wink Obviously I'm doing something completely wrong, but I don't know what. Somebody can help me out?

Get-VIServer xxx +-User +xxx +-Password +xxx

$MyHost = Get-VMHost xxx

$HostView = Get-View $MyHost.ID

$NetworkSystem = get-view $HostView.ConfigManager.NetworkSystem

$AllPortGroups = get-view $NetworkSystem.NetworkInfo.Portgroup | where {$_.HostPortGroupSpec.VlanId > 0 }

Foreach ($HostPortGroup in $AllPortGroups){

write-output $_.HostPortGroupSpec.Name

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This should work


Get-VIServer xxx +-User +xxx +-Password +xxx
$MyHost = Get-VMHost xxx
$HostView = Get-View $MyHost.ID
$NetworkSystem = get-view $HostView.ConfigManager.NetworkSystem
$AllPortGroups = $NetworkSystem.NetworkInfo.Portgroup | where {$_.Spec.VlanId -gt 0 }
foreach ($HostPortGroup in $AllPortGroups){
write-output $HostPortGroup.Spec.Name
}

Note1: Portgroup is an array not a MoRef

Note2: the property name is Spec not HostPortGroupSPec

Note3: for the comparison use -gt instead of the redirection operator


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

This should work


Get-VIServer xxx +-User +xxx +-Password +xxx
$MyHost = Get-VMHost xxx
$HostView = Get-View $MyHost.ID
$NetworkSystem = get-view $HostView.ConfigManager.NetworkSystem
$AllPortGroups = $NetworkSystem.NetworkInfo.Portgroup | where {$_.Spec.VlanId -gt 0 }
foreach ($HostPortGroup in $AllPortGroups){
write-output $HostPortGroup.Spec.Name
}

Note1: Portgroup is an array not a MoRef

Note2: the property name is Spec not HostPortGroupSPec

Note3: for the comparison use -gt instead of the redirection operator


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

Reply
0 Kudos
esloof
Expert
Expert
Jump to solution

You're really amazing, it works, many thanks for the correction, I was going to give you maximum points for the correct answer but clicked the wrong icon sorry about that.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem.

Glad to have helped.


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

esloof
Expert
Expert
Jump to solution

LucD,

Just one more question, when I try to pipe the output to a CSV file I only see one entry.

{write-output $HostPortGroup.Spec | Select-Object Name,VlanId | Export-Csv c:\test.txt}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This should create the CSV file.

Get-VIServer xxx +-User +xxx +-Password +xxx
$MyHost = Get-VMHost xxx
$HostView = Get-View $MyHost.ID
$NetworkSystem = get-view $HostView.ConfigManager.NetworkSystem
$AllPortGroups = $NetworkSystem.NetworkInfo.Portgroup | where {$_.Spec.VlanId -gt 0 }

$vlanReport = @()

foreach ($HostPortGroup in $AllPortGroups){
  $vlanInfo = "" | select-object Name, VLANId
  $vlanInfo.Name = $HostPortGroup.Spec.Name
  $vlanInfo.VLANId = $HostPortGroup.Spec.VlanId
  $vlanReport += $vlanInfo
}
$vlanReport | Export-Csv c:\test.csv

I used an array ($vlanReport) to store custom objects ($vlanInfo) for each portgroup.

Via the pipe the array is written to the CSV file


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

Reply
0 Kudos
esloof
Expert
Expert
Jump to solution

I received some odd error and created a workaround:

{$HostPortGroup.Spec | Select-Object Name,VlanId | Out-File -filepath "C:\portgroups.txt" -append}

Thanks for all the help.

Reply
0 Kudos