Compare commits

..

No commits in common. "0ce3ebdb3506c67a0c731e1fa54a45f171d34470" and "e5bc1f76dfa88e74e36cd09f51966f843be57d59" have entirely different histories.

3 changed files with 19 additions and 87 deletions

2
.gitignore vendored
View File

@ -71,5 +71,3 @@ fabric.properties
# Android studio 3.1+ serialized cache file # Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
# ignore text XML WOL database files
*.test.xml

View File

@ -3,7 +3,7 @@
<Configuration> <Configuration>
<BroadcastAddress>127.0.0.1</BroadcastAddress> <BroadcastAddress>127.0.0.1</BroadcastAddress>
<Port>7</Port> <Port>7</Port>
<DnsSuffix>.internal.mydomain.tld</DnsSuffix> <DnsSuffix>.internal.mydomain.net</DnsSuffix>
</Configuration> </Configuration>
<Computers> <Computers>
<Computer name="reception01" friendlyName="Front-desk computer" mac="aa:00:00:00:00:01"/> <Computer name="reception01" friendlyName="Front-desk computer" mac="aa:00:00:00:00:01"/>

View File

@ -6,25 +6,14 @@ param
# Computer to wake up # Computer to wake up
[Alias("Computer")] [Alias("Computer")]
[String] [String]
$TargetComputer = "", $targetComputer = "",
# XML database file containing computer details # Skip RDP connectivity readiness check
[Alias("List", "database", "db")] [Alias("Check", "Readiness")]
[String]
$WolDatabase = "WOLDatabase.xml",
# Skip connectivity readiness check
[Alias("NoCheck", "NoReadiness")]
[Switch] [Switch]
$NoConnectivityCheck = $false, $ConnectivityCheck = $false,
# Initial wait period in seconds before first connectivity check # RDP connectivity readiness check timeout
[Int] [Int]
$WaitBeforeConnectivityCheck = 10, $Timeout = 300
# connectivity readiness check total timeout
[Int]
$Timeout = 300,
# connectivity readiness port to check
[Alias("CheckPort")]
[Int]
$ConnectivityPort = 3389
) )
function exitError($errMessage, $PSItem, $exitCode = 1) function exitError($errMessage, $PSItem, $exitCode = 1)
@ -43,14 +32,8 @@ function exitError($errMessage, $PSItem, $exitCode = 1)
Exit $exitCode Exit $exitCode
} }
function exitGracefully() $wolModuleName = "wol-magicPacket"
{ $wolDatabase = "WOLDatabase.xml"
Write-Host -ForegroundColor Green "`r`nFinished.`r`n"
Exit 0
}
# constants
Set-Variable -Name wolModuleName -Value "wol-magicPacket" -Option Constant
# exit on error if Send-MagicPacket module is not loaded # exit on error if Send-MagicPacket module is not loaded
if (!(Get-Module -Name $wolModuleName)) if (!(Get-Module -Name $wolModuleName))
@ -67,38 +50,31 @@ if (!(Get-Module -Name $wolModuleName))
} }
# exit if database cannot be found/read # exit if database cannot be found/read
if (!(Test-Path -Path $WolDatabase -PathType Leaf)) if (!(Test-Path -Path $wolDatabase -PathType Leaf))
{ {
$errMessage = "Unable to find or read Wake-On-LAN database file ($WolDatabase)" $errMessage = "Unable to find or read Wake-On-LAN database file ($wolDatabase)"
exitError $errMessage exitError $errMessage
} }
# get target computer name if not already specified # get target computer name if not already specified
if ( [String]::IsNullOrWhiteSpace($TargetComputer)) if ( [String]::IsNullOrWhiteSpace($targetComputer))
{ {
do do
{ {
$TargetComputer = Read-Host -Prompt 'Computer to wake-up' $targetComputer = Read-Host -Prompt 'Computer to wake-up'
} while ( [String]::IsNullOrWhiteSpace($TargetComputer)) } while ( [String]::IsNullOrWhiteSpace($targetComputer))
} }
Write-Host Write-Host
# read database and assemble list of target computers # read database and assemble list of target computers
[xml]$db = Get-Content -Path $WolDatabase [xml]$db = Get-Content -Path $wolDatabase
$broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress $broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress
$port = $db.WOLDatabase.Configuration.Port $port = $db.WOLDatabase.Configuration.Port
$dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix $dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix
$targetComputers = [System.Collections.Generic.List[PSObject]]::new() $targetComputers = [System.Collections.Generic.List[PSObject]]::new()
$db.WOLDatabase.Computers.Computer | Where-Object { $_.name -match "$TargetComputer" } | ForEach-Object { $targetComputers.Add($_) } $db.WOLDatabase.Computers.Computer | Where-Object { $_.name -match "$targetComputer" } | ForEach-Object { $targetComputers.Add($_) }
$removeFromTargetComputers = [System.Collections.Generic.List[String]]::new() $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 # send WOL magic packets
$targetComputers | ForEach-Object { $targetComputers | ForEach-Object {
$wolError = @() $wolError = @()
@ -132,49 +108,7 @@ $removeFromTargetComputers | ForEach-Object {
$targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null $targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null
} }
# exit if connectivity check skipped or if no computers to test # process RDP readiness check
if ($NoConnectivityCheck) $targetComputers
{
Write-Host "`r`nSkipping connectivity checks."
exitGracefully
}
elseif ($targetComputers.Count -eq 0)
{
exitError "No successful wake-up packets sent" -exitCode 5
}
# wait for initialWaitTime seconds to let computer(s) wake up Exit 0
for ($i = $WaitBeforeConnectivityCheck; $i -gt 0; $i--) {
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -SecondsRemaining $i
Start-Sleep 1
}
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -Completed
# iterate computers and test connectivity
$targetComputers | ForEach-Object {
$name = $_.name
$friendlyName = $_.friendlyName
$fqdn = -join ($name, $dnsSuffix)
$connectionError = ""
if ( [String]::IsNullOrWhiteSpace($friendlyName))
{
Write-Host "Testing connection readiness of '$name'... " -NoNewline
}
else
{
Write-Host "Testing connection readiness of '$friendlyName'... " -NoNewline
}
if (!(Test-NetConnection -ComputerName $fqdn -Port $ConnectivityPort -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable connectionError))
{
Write-Host -ForegroundColor Red "[ERROR]"
Write-Host -ForegroundColor Red "`tAdditional information:", $connectionError[-1].ToString()
}
else
{
Write-Host -ForegroundColor Green "[OK]"
}
}
# exit gracefully
exitGracefully