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:
+92
@@ -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
|
||||
Reference in New Issue
Block a user