Find Hyper-V Virtual Machine by MAC Address – 60 Second Solutions

Today I had to try and find a virtual machine given its MAC address on a non-clustered Hyper-V environment. Here’s a quick PowerShell script to do just that!

import-module hyper-v

$Servers = @(“server1”,“server2”,“etc”)
$results = @()
foreach ($hvs in $Servers)
{
Get-VM –Computername $hvs | Get-VMNetworkAdapter | select VMName, MACAddress ,ComputerName | foreach-object {
$out = New-Object -TypeName PSObject -Property @{
VMName = $_.VMName
MACAddress = $_.MACAddress
Host = $_.ComputerName
}
$results += $out
}
}
$results | Out-GridView

Put your Hyper-V server names in the $servers variable and then run as a user who has remote access to each of those servers.

You’ll get a lovely output, that looks something like this: (obviously, without the horrific blurring!)

You can then use the filter box at the top to find your chosen VM – pretty handy!

PowerWhat Now?

As usual, let’s break this thing down line-by-line:

import-module hyper-v

We’re dealing with Hyper-V, so let’s add the Hyper-V commands to the current session

$Servers = @(“server1″,”server2″,”etc”)
$results = @()

Make two arrays, one with all our server names in, the other to store the results

foreach ($hvs in $Servers)

Loop through the servers..

Get-VM –Computername $hvs | Get-VMNetworkAdapter | select VMName, MACAddress ,ComputerName | ….

Get all the virtual machines on that server; pass the results; get each of the network cards in those servers and then just grab the vm’s name, the card’s mac address and the computer name that’s hosting it

…. foreach-object {
$out = New-Object -TypeName PSObject -Property @{

Take that output and for each network card, make a new PowerShell object stored in the variable $out

VMName = $_.VMName
MACAddress = $_.MACAddress
Host = $_.ComputerName

Give it the following properties

$results += $out

Add the result to our results variable

$results | Out-GridView

And show them as a nice grid!

Leave a Reply

Your email address will not be published. Required fields are marked *