Home > ESX, PowerCLI, vSphere > Get all VMs with Fixed MAC Addresses using PowerCLI

Get all VMs with Fixed MAC Addresses using PowerCLI

I needed a list of all VMs with a Fixed MAC Address running on vSphere (it works for ESX 3.5 as well).

Let’s build up this command from the beginning.  We start with Get-VM to get a hold of all VMs.

Get-VM

This of course will list just any VM.  So we need some filtering.

By piping the output of Get-VM into the Get-NetworkAdapter cmdlet, we get some usefull information about the vNICs.

Get-VM | Get-NetworkAdapter

image

A lot of vNIC info is present, but no details about the MAC Address.  We need to dig a little deeper into the vNIC object by using the ExtensionData property.  You can access all the properties of the underlying object through this ExtensionData property.  And we are interested in the AddressType property:

Get-VM | Get-NetworkAdapter | Foreach-Object {$_.ExtensionData.AddressType}

image

This will tell you for each vNIC if it’s Assigned (auto generated MAC) or Manual (fixed MAC).

Okay, so now that we know where the info lies, let’s put this in a nice one-liner:

Get-VM | Where-Object {($_ | Get-NetworkAdapter).ExtensionData.AddressType -eq "manual"}

image

Looking better already.  But what if we want to display the VM Name and the MAC Address as well.

Get-VM | Where-Object {($_ | Get-NetworkAdapter).ExtensionData.AddressType -eq "manual"} | `
Select-Object -Property Name, PowerState, @{"Name"="MAC";"Expression"={($_ | Get-NetworkAdapter).MacAddress}}

image

Et voilà!

Advertisements
Categories: ESX, PowerCLI, vSphere
  1. August 3, 2012 at 18:00

    This example made me understand how to extract what I *really* need using PowerCLI!

    Thanks a bunch 😀

  2. Chaitanyakumar
    April 5, 2013 at 13:46

    Thanks dude…. really helpful.. keep posting abt powercli….

  3. Chaitanyakumar
    April 5, 2013 at 14:20

    Here, we cant get the name of the VM, is there any way where we can get the name of the vm in get-vm|get-networkadapter command?

    • April 14, 2013 at 19:28

      In the last code example, you have the code that lists the name of the VM as well!

      Get-VM | Where-Object {($_ | Get-NetworkAdapter).ExtensionData.AddressType -eq “manual”} | `
      Select-Object -Property Name, PowerState, @{“Name”=”MAC”;”Expression”={($_ | Get-NetworkAdapter).MacAddress}}

  4. taliz
    August 27, 2013 at 13:01

    Seems like this does not work in PowerCLI 5.1 😦
    There is no ExtensionData property of Get-NetworkAdapter anymore.

    • taliz
      August 28, 2013 at 11:20

      Actually it seems the problem might be with PowerCLI 5.1 to a 4.1 vCenter. That property just doesn’t exist. It works fine with a 5.1 vCenter.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: