I am trying to get a list of all VM's in a given VCenter, with thier VLAN's. Can anyone help?
Could it be that your VMs are connected to dvSwitches instead of regular vSwitches ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
foreach($vm in Get-VM ){ Get-VirtualPortGroup -VM $vm |
Select @{N="VM";E={$vm.Name}},VlanId
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is great. It brings back the VM's with a placeholder for the VlanId, but they are all blank. Any ideas?
Could it be that your VMs are connected to dvSwitches instead of regular vSwitches ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry,
Yes, they are all connected to dvSwitches
That's a bit trickier, try this version.
It should give the VlanId independent what type of switch the VM is connected to
foreach($vm in Get-VM ){ Get-VirtualPortGroup -VM $vm | Select @{N="VM";E={$vm.Name}},
@{N="VlanId";E={ if($_.ExtensionData -is [VMware.Vim.DistributedVirtualPortgroup]){ $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
} else{ $_.VlanId
} }} }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This one should be better, it handles regular VLANs and private VLANs.
foreach($vm in Get-VM ){ Get-VirtualPortGroup -VM $vm |
Select @{N="VM";E={$vm.Name}},
@{N="VlanId";E={ if($_.ExtensionData -is [VMware.Vim.DistributedVirtualPortgroup]){ if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId){ $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
} else{ $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
} } else{ $_.VlanId
} }} }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect. Your a Genius. Thank you very much. I have been working on this for 3 days on and off. Now to export it.
Export-Csv "C:\VLAN-report.csv" -NoTypeInformation -UseCulture
Should work I hope. Thank you very much again.
Since the Select is inside a ForEach loop, you'll have to use a little trick.
This is one way of doing it
&{foreach($vm in Get-VM){ Get-VirtualPortGroup -VM $vm |
Select @{N="VM";E={$vm.Name}},
@{N="VlanId";E={ if($_.ExtensionData -is [VMware.Vim.DistributedVirtualPortgroup]){ if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId){ $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
} else{ $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
} } else{ $_.VlanId
} }} }} | Export-Csv c:\report.csv -NoTypeInformation -UseCulture
In short, we execute the whole script as a code-block and then we can pipe the result of that execution to the CSV
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That works perfectly. One last thing. The VM's have multiple NIC's. Any quick answer on how to put the second NIC's in a seperate column?
What's the maximum number of NICs a VM can have ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
3. I went through and manually sorted them (all 150 or so for the first vcenter). I don't want to bother you with the little details. You have been a life saver already.
Sent from mobile device