ps-wakeup/wakeup.ps1

121 lines
3.3 KiB
PowerShell

<#
#>
param
(
# Computer to wake up
[Alias("Computer")]
[String]
$targetComputer = "",
# Skip RDP connectivity readiness check
[Alias("Check", "Readiness")]
[Switch]
$ConnectivityCheck = $false,
# RDP connectivity readiness check timeout
[Int]
$Timeout = 300
)
function exitError($errMessage, $PSItem, $exitCode = 1)
{
if ( [String]::IsNullOrEmpty($errMessage))
{
Write-Host -ForegroundColor Red "`n`rABNORMAL EXIT: exitError function called without exit message!`n`r"
exit 99
}
Write-Host -ForegroundColor Red "`r`nERROR: ${errMessage}."
if ($PSItem)
{
Write-Host -ForegroundColor Red "`r`nAdditional information:`r`n$PSItem"
}
Write-Host
Exit $exitCode
}
$wolModuleName = "wol-magicPacket"
$wolDatabase = "WOLDatabase.xml"
# exit on error if Send-MagicPacket module is not loaded
if (!(Get-Module -Name $wolModuleName))
{
try
{
Import-Module -Name $wolModuleName -ErrorAction Stop
}
catch
{
$errMessage = "Unable to load '$wolModuleName'"
exitError $errMessage $PSItem
}
}
# exit if database cannot be found/read
if (!(Test-Path -Path $wolDatabase -PathType Leaf))
{
$errMessage = "Unable to find or read Wake-On-LAN database file ($wolDatabase)"
exitError $errMessage
}
# get target computer name if not already specified
if ( [String]::IsNullOrWhiteSpace($targetComputer))
{
do
{
$targetComputer = Read-Host -Prompt 'Computer to wake-up'
} while ( [String]::IsNullOrWhiteSpace($targetComputer))
}
Write-Host
# read database and assemble list of target computers
[xml]$db = Get-Content -Path $wolDatabase
$broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress
$port = $db.WOLDatabase.Configuration.Port
$dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix
$targetComputers = [System.Collections.Generic.List[PSObject]]::new()
$db.WOLDatabase.Computers.Computer | Where-Object { $_.name -match "$targetComputer" } | ForEach-Object { $targetComputers.Add($_) }
$removeFromTargetComputers = [System.Collections.Generic.List[String]]::new()
# exit if nothing to do (i.e. empty targetComputers list)
if ($targetComputers.Count -eq 0)
{
Write-Host -ForegroundColor Yellow "No computers found matching '$targetComputer'. Nothing to do.`r`n"
exit 0
}
# send WOL magic packets
$targetComputers | ForEach-Object {
$wolError = @()
$name = $_.name
$friendlyName = $_.friendlyName
if ( [String]::IsNullOrWhiteSpace($friendlyName))
{
Write-Host "Processing request to wake-up '$name'... " -NoNewline
}
else
{
Write-Host "Processing request to wake-up '$friendlyName'... " -NoNewline
}
# send magic packet
$_.mac | Send-MagicPacket -BroadcastIP $broadcastIP -Port $port -ErrorAction SilentlyContinue -ErrorVariable +wolError
if ($wolError.Count -eq 0)
{
Write-Host -ForegroundColor Green "[OK]"
}
else
{
Write-Host -ForegroundColor Red "[ERROR]"
$removeFromTargetComputers.Add($name)
}
}
# remove computers from targetComputers if WOL packet was not successfully sent
$removeFromTargetComputers | ForEach-Object {
$removeName = $_
$targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null
}
# process RDP readiness check
$targetComputers
Exit 0