From 939ff159f50eafa8ab361131ed4546f0d849984d Mon Sep 17 00:00:00 2001 From: Asif Bacchus Date: Sun, 5 Sep 2021 17:06:38 -0600 Subject: [PATCH] struct(wakeup): basic WOL invocation script - generate dummy database of computers - basic script to wake up computers found in database --- WOLDatabase.xml | 37 +++++++------------- wakeup.ps1 | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 25 deletions(-) diff --git a/WOLDatabase.xml b/WOLDatabase.xml index 4f4acab..1e6bd72 100644 --- a/WOLDatabase.xml +++ b/WOLDatabase.xml @@ -1,28 +1,15 @@ - - - - - + + 127.0.0.1 + 7 + .internal.mydomain.net + + + + + + + + \ No newline at end of file diff --git a/wakeup.ps1 b/wakeup.ps1 index e69de29..16f0919 100644 --- a/wakeup.ps1 +++ b/wakeup.ps1 @@ -0,0 +1,92 @@ +<# +#> + +param +( +# Computer to wake up + [Alias("Computer")] + [String] + $targetComputer = "" +) + +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 +} + +# find computer in database +[xml]$db = Get-Content -Path $wolDatabase +$broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress +$port = $db.WOLDatabase.Configuration.Port +$dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix + +$db.WOLDatabase.Computers.Computer | Where-Object { $_.name -match "$targetComputer" } | ForEach-Object { + if ( [String]::IsNullOrWhiteSpace($_.friendlyName)) + { + $name = $_.name + Write-Host "Processing request to wake-up '$name'... " -NoNewline + } + else + { + $friendlyName = $_.friendlyName + Write-Host "Processing request to wake-up '$friendlyName'... " -NoNewline + } + + # send magic packet + $_.mac | Send-MagicPacket -BroadcastIP $broadcastIP -Port $port -WarningAction SilentlyContinue -WarningVariable wolErr -ErrorAction SilentlyContinue -ErrorVariable wolErr + if ($wolErr.Count -eq 0) + { + Write-Host -ForegroundColor Green "[OK]" + } + else + { + Write-Host -ForegroundColor Red "[ERROR]" + } +} + +Exit 0 \ No newline at end of file