Wake on LAN for Hyper-V Guests

When you turn your VM off, it’s off.  As in off-off, off: equivalent to powering it off and pulling out all the cables.  This is why you can’t use Wake on LAN (WoL) for Hyper-V guests because the network adaptor for the VM is off-off.  Which is annoying if you have a bunch of VMs that you use on a regular basis; to boot them all you have to first RDP onto the Hyper-V host and then boot each one in turn – I know, I know, a real first world problem – but it annoys me!  There are a couple of products available to help with this but they are all pretty overpriced in my opinion, especially as they provide something so simple.

WoL is pretty simple: it’s a UDP broadcast to the entire network, usually on port 7, using something called a “magic packet”.  This packet contains the MAC address of the computer to start (amongst other data).  A network adaptor listens out for these packets and reacts by powering on the computer; this all happens within the network card and the computer never gets involved (it’s off-off remember).  WoL doesn’t wait for a response, there’s no encryption/security for it, and it has no additional options/configurations other than this.

This simplicity is great news though because it means that PowerShell can be used to recreate the missing WoL functionality for Hyper-V guests.  The below script sits on your Hyper-V host listening for magic packets.  When it receives one it checks to see if the MAC Address contained matches the MAC Address of one of the VMs that it is hosting, if so it turns it on.  Simple.

With this script I can now take out my Windows Phone, open the Wake-on-LAN app, send the magic packets, and boot up my entire lab before I’ve even made it up the stairs to my office   🙂

untitled

Set the script to run permanently on your Hyper-V host, as per the above screenshot.  Upon start the code will enumerate the MAC Addresses for all VMs detected locally and will then sit in the background reading any magic packet that it sees go out across the network and react accordingly.  All informational and error messages are written out to the console window.
Prerequisites:
  • UDP port 7 should opened on your Windows Server firewall – not usually a requirement because WoL is processed by the hardware, but here we are having to process it within the operating system.
  • PowerShell execution policy will need to be set to allow the script to run
  • The script currently runs in a logged on session, so a user session needs to stay open (I’ll attempt to address this in future versions).  I have my server logged on and locked to accommodate this.
  • A WoL app on your phone and/or computer to send the requests.  Some apps require specifying the IP address of the computer as well as it’s MAC Address.  If this is the case you can either specify the IP of your Hyper-V host or use 255.255.255.255.
The script is very much a work in progress project at the moment, but please try it and feedback to me any issues you have, or any other feedback, via the comments at the bottom.  As I improve it I’ll update this blog.  I’ve only tested the script on Windows Server 2016, using a non-admin account.
Advertisements

6 thoughts on “Wake on LAN for Hyper-V Guests

    • On my Windows 10 Mobile I’m using “Wake on LAN (Magic Packet)” as it allows me to pin individual devices to the start screen – and it works with Continuum as well! But I’ve tested it on most of the free apps in the store.

      Let me know how you get on with the script, I’m working on an updated version of the script to fix a few formatting bugs and add some better output, I hope to have it uploaded (to the same link as listed above) in the next few days.

  1. For me it doesn’t work. The Script detects the VMs but then nothing happens…
    I tried to disable the Firewall, use the Wake On Lan on Openwrt Router and an app on my Windows Phone 8.1…

  2. @Michael – it should work fine, have you tried the app I mention in the comment above? Also, try temporarily disabling the firewall on your server, and double check that both the server and the phone are connected to the same network and not your 3G network

  3. Hi Daniel,
    i found the solution:
    After a short Wireshark Capture i detected that the Magic Packet used port 9 not port 7 🙂

Speak your mind...

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