struct(wakeup): basic WOL invocation script

- generate dummy database of computers
- basic script to wake up computers found in database
This commit is contained in:
Asif Bacchus 2021-09-05 17:06:38 -06:00
parent f0ddf5ab5a
commit 939ff159f5
2 changed files with 104 additions and 25 deletions

View File

@ -1,28 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<WOLDatabase> <WOLDatabase>
<Computer <Configuration>
name="reception01" <BroadcastAddress>127.0.0.1</BroadcastAddress>
description="Front-desk computer" <Port>7</Port>
mac="aa:00:00:00:00:01" <DnsSuffix>.internal.mydomain.net</DnsSuffix>
/> </Configuration>
<Computer <Computers>
name="accounting01" <Computer name="reception01" friendlyName="Front-desk computer" mac="aa:00:00:00:00:01"/>
description="accounting workstation 1" <Computer name="accounting01" friendlyName="accounting workstation 1" mac="bb:00:00:00:00:01"/>
mac="bb:00:00:00:00:01" <Computer name="accounting02" friendlyName="" mac="bb:00:00:00:00:02"/>
/> <Computer name="manager01" friendlyName="management workstation 1" mac="cc:00:00:00:00:01"/>
<Computer <Computer name="tech" friendlyName="I.T. technician workstation" mac="ff:ff:ff:ff:ff:01"/>
name="accounting02" </Computers>
description="accounting workstation 2"
mac="bb:00:00:00:00:02"
/>
<Computer
name="manager01"
description="management workstation 1"
mac="cc:00:00:00:00:01"
/>
<Computer
name="techstation"
description="I.T. technician workstation"
mac="ff:ff:ff:ff:ff:01"
/>
</WOLDatabase> </WOLDatabase>

View File

@ -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