VMware Cloud Community
JoeCap23
Contributor
Contributor
Jump to solution

All VM's with VLAN's

I am trying to get a list of all VM's in a given VCenter, with thier VLAN's. Can anyone help?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Could it be that your VMs are connected to dvSwitches instead of regular vSwitches ?


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

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

JoeCap23
Contributor
Contributor
Jump to solution

This is great. It brings back the VM's with a placeholder for the VlanId, but they are all blank. Any ideas?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that your VMs are connected to dvSwitches instead of regular vSwitches ?


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

0 Kudos
JoeCap23
Contributor
Contributor
Jump to solution

Sorry,

Yes, they are all connected to dvSwitches

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
JoeCap23
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
JoeCap23
Contributor
Contributor
Jump to solution

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?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What's the maximum number of NICs a VM can have ?


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

0 Kudos
JoeCap23
Contributor
Contributor
Jump to solution

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

0 Kudos