Skip navigation
PowerShell Script Wakes Remote Machines as a PowerShell Function

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

   

$data = Get-Content "$PSScriptRoot\wakeup.dat"
    write-debug $data.count #total lines read from file
   
    $datahash = @{}
   
    foreach ($line in $data)
    {
        $linesplit = $line.split("|")
        if ($linesplit.Length -ne 2)
        {
         throw 'Each line should have | format, e.g. host|00:23:23:00:23:23'
        }
        else
        {
            $datahash.Add($linesplit[0].ToUpper(),$linesplit[1])
        }
    }

    foreach ($computername in $computers)
{
        $HostName = $computername.ToUpper() #hash tables are not case sensitive but just to be sure!
        $MACStr=$null
        $MACStr=$datahash.Get_Item($HostName)

        if ($MACStr -ne $null) #if it found entry
        {
            $FoundMatch = $TRUE

            $MACAddr = $MACStr.split(':') | %{ [byte]('0x' + $_)}
            $MACAddrParts = $MACStr.split(':')
            if ($MACAddrParts.Length -ne 6)
            {
             throw 'MAC address must be format xx:xx:xx:xx:xx:xx'
            }
            $UDPclient = new-Object System.Net.Sockets.UdpClient
            $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
            $packet = [byte[]](,0xFF * 6)
            $packet += $MACAddr * 16
            [void] $UDPclient.Send($packet, $packet.Length)
            write "Wake-On-Lan magic packet sent to $MACStr, length $($packet.Length)"
        }
        else
        {
            write-host "Never found match for " $HostName
        }
    }
}

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

 

PS D:\> Import-Module wakeup

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

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish