Home > Software engineering >  Can powercli tell me what the VM Name is when listing network adapters for vms?
Can powercli tell me what the VM Name is when listing network adapters for vms?

Time:12-13

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?

CodePudding user response:

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

  • Related