Dear All,
I need to find a way to display and export all vlans on all hosts. I am using Standard Switches. I require to have the table like this format.
HostName, VLanName, VirtualSwitchName, VLanID
I am able to get he details of individual hosts with the below command,
Get-VirtualPortGroup -VMHost hostname | ft Name, VirtualSwitch, VLanID
But the output does not show the Hostname.
I tried the command,
Get-VMHost | Get-VirtualPortGroup | ft Name, VirtualSwitch, VLanID
But still it is not showing the hostname on the output. If anybody know the PoweShell to create the required output, please help me out.
Regards
Rajesh
Sorry, missed a closing curly brace in the copy/paste.
The code above has been corrected, please try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this, it should work for regular portgroups and portgroups on distributed switches.
foreach($esx in Get-VMHost){
Get-VirtualPortGroup -VMHost $esx |
Select @{N='VMHost';E={$esx.Name}},
Name,
@{N="VlanId";E={
if($_.ExtensionData -is [VMware.Vim.DistributedVirtualPortgroup]){
if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [Array]){
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{
$_.Start.ToString() + "-" + $_.End.ToString()
}
}
elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId){
$_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
}
else{
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
}
}
else{
$_.VlanId
}}}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry, missed a closing curly brace in the copy/paste.
The code above has been corrected, please try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LucD,
Great!!!
I really appriciate your efforts. The script worked.
Regards
Rajesh
Hi There , Can you help me to have complete this script not understanding where parenthesis missing
Many thanks for the script... Works beautifully in our setup... Output very informative.
BUT:
Is there a way to direct the screen output to a CSV fle, for easier slicing & dicing?
The iterative nature of the script [plus my newbie statue with PowerShell] is preventing easy use of Export-CSV.
Thanks in advance...
You could do something like this
$report = foreach($esx in Get-VMHost){
Get-VirtualPortGroup -VMHost $esx |
Select @{N='VMHost';E={$esx.Name}},
Name,
@{N="VlanId";E={
if($_.ExtensionData -is [VMware.Vim.DistributedVirtualPortgroup]){
if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [Array]){
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{
$_.Start.ToString() + "-" + $_.End.ToString()
}
}
elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId){
$_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
}
else{
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
}
}
else{
$_.VlanId
}}}}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
One word: Fabulous!!!
This is a perfect, one-trick pony that helps me reconcile our VLAN documentation vs. actual usage.
Aside from customizing destination, this is/was ready-to-go.
Many thanks, and Kudos...