This script allows you to wake a computer up by using the host name. a local DNS and Microsoft DHCP server are required. If this script is run from a workstation, RSAT will need to be installed. The script must be run as administrator.

Most WOL software and scripts require you to enter in the IP address and MAC address of the host. This script eliminates to need for either. I needed something that will allow my techs to be able to wake a computer up without having to lookup the IP address and MAC address in the DHCP pool.

 

PowerShell
Edit|Remove
### 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
}