Wake on LAN using Host Name

If you have ever used WOL (Wake on LAN) you know how tedious it gets to retrieve the necessary information for WOL to work. Traditionally with WOL, you must have the IP address and the MAC address of the device to send the magic packet. At times getting this information can be a bit of an ordeal considering you have to look up the information you need from within your DHCP server. 

This script is designed to allow you to send the magic packet to a computer without the IP address or MAC address. All you need is the computer name. 

The script does require that RSAT is installed on the machine the script is running on. The script queries DHCP and extracts the IP address and MAC address of the machine. Once it has these two pieces of information, a magic packet is set out to the computer. The attempt intervals can be modified as well as the intervals between tries. 

If you would prefer this script has also been posted on Microsoft TechNet for download. https://gallery.technet.microsoft.com/Wake-on-LAN-via-Host-Name-a2d00b67?redir=0


### WWW.ITLUMBERJACK.COM ###
######### 9-16-17 ##########

#########################
### VARIABLES to EDIT ###
#########################

### DHCP SERVER to QUERY ###
$DHCP_Server = 'DHCP_SVR.localdomain.local'

### NUMBER of TIMES TO SEND MAGIC PACKET ###
$Attempts = '5'

### PAUSE BETWEEN ATTEMPTS ###
$Sleep = '2'

### WAKE ON LAN PORT NUMBER ###
$WOL_Port = '9'

####################################
### DO NOT EDIT BELOW THIS POINT ###
####################################

### PROMPT USER FOR HOST NAME ###

$Host_Name = Read-Host -prompt "Host Name"

### RESOLVE DNS NAME ###

$DNS_Resolution = resolve-dnsname -name $Host_Name

### DHCP SERVER QUERY ###

$DHCP_Leases = Get-DhcpServerv4Scope -computername $DHCP_Server | Get-DhcpServerv4Lease -computername $DHCP_Server

### CLIENT INFO from DHCP ###

$Host_Info = $DHCP_Leases | Where-Object IPAddress -eq $DNS_Resolution.IPAddress

### IP ADDRESS as VARIABLE ###

$IPAddress = $Host_Info.IPAddress

### MAC ADDRESS as VARIABLE ###

$MAC_Address = $Host_Info.ClientId

### WOL FUNCTION ###
### Credit for Send-WOL Function: bit.ly/2DO3ccL
function Send-WOL
{

[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) 

}

### PING TEST ###

$Ping_Test = Test-Connection -ComputerName $IPAddress -Quiet

### START of MAGIC PACKET ###

$Attempt = 1

while ($Ping_Test -eq $false -and $Attempt -le $Attempts) {
        Write-Host "Sending Magic Packet...Attempt - " $Attempt -ForegroundColor Yellow
        Send-WOL -mac $MAC_Address -ip $IPAddress -port $WOL_Port
        Start-Sleep $Sleep
        $Ping_Test = Test-Connection -ComputerName $IPAddress -Quiet
        $Attempt++
}       

### MESSAGES ###

if ($Ping_Test -eq $true){ 
    Write-Host ""
    Write-Host "###############" -ForegroundColor Green
    Write-Host "Host is Online!" -ForegroundColor Green
    Write-Host "###############" -ForegroundColor Green
}

else {
    Write-Host ""
    Write-Host "########################################" -ForegroundColor Red
    Write-Host "Host is still offline. Please try again." -ForegroundColor Red
    Write-Host "########################################" -ForegroundColor Red
}