Saving electricity in your home lab using Powershell – part 3. Using WOL to wake the lab on demand

Introduction

In the previous blog posts we learned how to run scheduled tasks to save (hibernate) all running virtual machines and then power off the hyperv lab to save electricity.  Next we added another task/script to resume all the previously saved virtual machines that you were working with.

In this part we’ll prepare the host for Wake Up On Lan (WOL) so that we can wake it remotely on demand.

part 1. Automating graceful shutdown
part 2. Starting the previously running vms after power on
part 3. Using WOL to wake the lab on demand <- you are here

Preparing the hyper-v lab for WOL

On your hyper-v lab, open device manager, select the onboard NIC (network card) and bring up it’s properties, make sure that Wake Up on Magic Packet (or WOL) is enabled.

You might also need to verify that in the Power Management settings of the NIC properties.

UEFI/Bios settings

If the computers BIOS/UEFI settings has WOL options, make sure they are enabled.

Registry settings

Lastly, you may need to manually enable the following registry key (I had to on my HP hyper-v lab)

  1. Press Win + x, select Run and type regedit in the run line.
  2. Select OK to launch the Registry Editor.
  3. Navigate to KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NDIS\Parameters.
  4. Right click on Parameters.
  5. Select New DWORD (32-bit) Value.
  6. Add the key AllowWakeFromS5.
  7. Set the value to 1.
  8. Close the Registry Editor.
  9. Restart the computer.

Testing WOL

Now that you’ve enabled the lab for Wake Up On Lan it’s time to test it, I found a Powershell script online so I borrowed it, all it required was the MAC address of the NIC you want to wake up, and it’s IP Address.

Here’s the script (including the address of where I found it).

function Send-WOL
{
<# 
# via https://stackoverflow.com/questions/46649813/running-a-wol-powershell-script-through-a-batch-file
.SYNOPSIS 
Send a WOL packet to a broadcast address
.PARAMETER mac
The MAC address of the device that need to wake up
.PARAMETER ip
The IP address where the WOL packet will be sent to
.EXAMPLE 
Send-WOL -mac 00:11:32:21:2D:11 -ip 192.168.8.255 
#>

[CmdletBinding()]
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$mac,
[string]$ip="255.255.255.255", 
[int]$port=9
)
$broadcast = [Net.IPAddress]::Parse($ip)

$mac=(($mac.replace(":","")).replace("-","")).replace(".","")
$target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
$packet = (,[byte]255 * 6) + ($target * 16)

$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect($broadcast,$port)
[void]$UDPclient.Send($packet, 102)

}

send-wol -mac c8-d3-ff-a7-01-06 -ip 192.168.100.215

At the end of the script you can see that it calls the send-wol function with two parameters, the mac address and the ip address.

To test this, edit the script and insert your mac address and IP address of the lab you want to wake up.

Then power off the Hyper-v lab using the scheduled task created in part 1 and when it’s off, run the above script on another running computer on the same network. In my case it powers on the lab immediately and it then resumes whatever vm’s I had running using the script/scheduled task in part 2.

Well that’s all for this wee mini-series on saving money in your home lab, I hope you found it useful !

cheers

niall

 

This entry was posted in hyper-v, PowerShell. Bookmark the permalink.

One Response to Saving electricity in your home lab using Powershell – part 3. Using WOL to wake the lab on demand

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.