1

I want to get a list of VMs with a given vlan name configured so that when I am rolling back a vlan, with ACI I am certain that it is gone.

This script works, I connect to the vcenter with powercli and pass in a vlan_name:

foreach ($vm in Get-VM){
  $nic = Get-NetworkAdapter -VM $vm.name
  if ( $nic.NetworkName -eq "{{ vlan_name }}" ){
    echo $vm.name
  }
}

The problem is, it is an O(n) sort of algorithm and takes a long time to run (I have thousands of VMs and hundreds of vlans)

The annoying thing is

Get-VM | Get-NetworkAdapter

lists all the vlan's quickly, but doesn't output the vm names.

Is there a way I can get the VM use by Network Adapter?

Peter Turner
  • 11,199
  • 10
  • 68
  • 109
  • Just a guess as I can't test this locally, but does ```echo $nic.Parent.Name``` work instead of ```echo $vm.Name```? See https://developer.vmware.com/docs/powercli/latest/vmware.vimautomation.core/structures/vmware.vimautomation.vicore.types.v1.virtualdevice.networkadapter/ – mclayton Dec 12 '22 at 19:30

1 Answers1

1

This PowerShell lists out the VM name, the network adapter, and the type of network it's connected.

PowerShell

Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}}, Name, Type; 

or

Get-VM | Get-NetworkAdapter | Select-Object Parent, Name, Type;

Sample Output

VM                      Name                 Type
--                      ----                 ----
fserver3                Network adapter 1 Vmxnet3
pserver2                Network adapter 1 Vmxnet3
hserver2                Network adapter 1 Vmxnet3
lserver22               Network adapter 2 Vmxnet3
server1                 Network adapter 1 Vmxnet3
server2                 Network adapter 1 Vmxnet3

PowerShell (Filter Network Name)

Get-VM | Get-NetworkAdapter | Where-Object {$_.Name -eq "vlan_name"} | Select-Object @{N="VM";E={$_.Parent.Name}},Name,Type;

Supporting Resources

Bitcoin Murderous Maniac
  • 1,209
  • 1
  • 14
  • 27
  • 1
    thanks, I'm testing it out. I realized when writing this question that all I really needed was evidence that the network name was in use. – Peter Turner Dec 12 '22 at 20:34