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?
This PowerShell lists out the VM name, the network adapter, and the type of network it's connected.
Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}}, Name, Type;
or
Get-VM | Get-NetworkAdapter | Select-Object Parent, Name, Type;
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
Get-VM | Get-NetworkAdapter | Where-Object {$_.Name -eq "vlan_name"} | Select-Object @{N="VM";E={$_.Parent.Name}},Name,Type;