PowerShell Script Wakes Remote Machines as a PowerShell Function

Q: Do you have a version of your Windows PowerShell script to wake remote machines as a PowerShell function?

A: I took my PowerShell script from my FAQ "How Can I Easily Send a Magic Packet to Wake a Machine on My Subnet?" that wakes a remote machine and turned it into a PowerShell module file with a function, Send-Wakeup.

Save the content below to wakeup.psm1 in your PowerShell module path in a subfolder called wakeup, and then import the module for access to Send-Wakeup.

I also took the time to add in some Help, add support for multiple machines, and tidy up the code. Make sure the wakeup.dat file is in the same folder as the psm1 folder.

For details on the path to use for custom modules see FAQ "Q. How can I convert my PowerShell script into a module that works like a standard cmdlet?"

function Send-Wakeup<br>
{<br>
<#<br>
.SYNOPSIS<br>
Wakes a remote machine based on machine name<br>
.DESCRIPTION<br>
Wakes a remote machine based on machine name using MAC address from the wakeup.dat file in the same folder as the module.<br>
.PARAMETER computer<br>
Computer name that matches entry in the configuration file<br>
.EXAMPLE<br>
Send-Wakeup machine1,machine2<br>
#><br>
[cmdletbinding()]<br>
Param(<br>
[Parameter(ValuefromPipeline=$true,Mandatory=$true)][string[]]$computers)

   

$data = Get-Content "$PSScriptRoot\wakeup.dat"<br>
    write-debug $data.count #total lines read from file<br>
    <br>
    $datahash = @{}<br>
    <br>
    foreach ($line in $data)<br>
    {<br>
        $linesplit = $line.split("|")<br>
        if ($linesplit.Length -ne 2)<br>
        {<br>
         throw 'Each line should have <host>|<mac> format, e.g. host|00:23:23:00:23:23'<br>
        }<br>
        else<br>
        {<br>
            $datahash.Add($linesplit[0].ToUpper(),$linesplit[1])<br>
        }<br>
    }<br><br>
    foreach ($computername in $computers)<br>
{<br>
        $HostName = $computername.ToUpper() #hash tables are not case sensitive but just to be sure!<br>
        $MACStr=$null<br>
        $MACStr=$datahash.Get_Item($HostName)<br><br>
        if ($MACStr -ne $null) #if it found entry<br>
        {<br>
            $FoundMatch = $TRUE<br><br>
            $MACAddr = $MACStr.split(':') | %{ [byte]('0x' + $_)}<br>
            $MACAddrParts = $MACStr.split(':') <br>
            if ($MACAddrParts.Length -ne 6)<br>
            {<br>
             throw 'MAC address must be format xx:xx:xx:xx:xx:xx'<br>
            }<br>
            $UDPclient = new-Object System.Net.Sockets.UdpClient<br>
            $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)<br>
            $packet = [byte[]](,0xFF * 6)<br>
            $packet += $MACAddr * 16<br>
            [void] $UDPclient.Send($packet, $packet.Length)<br>
            write "Wake-On-Lan magic packet sent to $MACStr, length $($packet.Length)"<br>
        }<br>
        else<br>
        {<br>
            write-host "Never found match for " $HostName<br>
        }<br>
    }<br>
}</mac></host>

Below is an example of the import of the module, then using it.

 

PS D:\><strong> Import-Module wakeup</strong><br><br>
PS D:\> S<strong>end-Wakeup savdalhv01,savdalhv02,savdalhv03,awf</strong><br>
Wake-On-Lan magic packet sent to 00:15:17:C4:E1:05, length 102<br>
Wake-On-Lan magic packet sent to 14:DA:E9:41:BC:4D, length 102<br>
Wake-On-Lan magic packet sent to 1C:6F:65:31:2C:DB, length 102<br>
Never found match for AWF

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.