VMware Cloud Community
StewartBeel
Contributor
Contributor
Jump to solution

Exporting VLan Id to CSV

Hi

I'm running this script which outputs some basic information on all the VMs in my VC, which I found somewhere on the VMware forums, however the VLan ID information is blank for all VMs.

How can I get this to work so it shows the correct Vlan ID?

Connect-VIServer vc
Get-VM | Select Name,Notes,
    @{N="IP addr";E={[string]::Join(',',$_.Guest.IPAddress)}},
    @{N="Portgroup";E={[string]::Join(',',($_.Guest.Nics | %{$_.NetworkName}))}},
    @{N="VLANid";E={(Get-VirtualPortGroup -Name $_.NetworkName -VM $_).VLanId}} |
Export-Csv "C:\Temp\ServerInfo.csv" -NoTypeInformation
Please help.
Thanks,
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try it like this

Connect-VIServer vc 
Get-VM
| Select Name,Notes,
    @{N="IP addr";E={[string]::Join(',',$_.Guest.IPAddress)}},
    @{N="Portgroup";E={[string]::Join(',',($_.Guest.Nics | %{$_.NetworkName}))}},
    @{N="VLANid";E={[string]::Join(',',(Get-VirtualPortGroup -VM $_ | %{$_.VLanId}))}} | Export-Csv "C:\Temp\ServerInfo.csv" -NoTypeInformation

In this case there is no need to pass the NetworkName since you use the VM parameter.

For each returned Portgroup, the script extracts the VLanId.


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Try it like this

Connect-VIServer vc 
Get-VM
| Select Name,Notes,
    @{N="IP addr";E={[string]::Join(',',$_.Guest.IPAddress)}},
    @{N="Portgroup";E={[string]::Join(',',($_.Guest.Nics | %{$_.NetworkName}))}},
    @{N="VLANid";E={[string]::Join(',',(Get-VirtualPortGroup -VM $_ | %{$_.VLanId}))}} | Export-Csv "C:\Temp\ServerInfo.csv" -NoTypeInformation

In this case there is no need to pass the NetworkName since you use the VM parameter.

For each returned Portgroup, the script extracts the VLanId.


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

0 Kudos
StewartBeel
Contributor
Contributor
Jump to solution

Genius!!

Thank you so much Smiley Happy

I'm a PowerCLI novice

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, we all started there :smileygrin:

Keep playing and experimenting!


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

0 Kudos