Compare commits
14 Commits
e5bc1f76df
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| cccd7ca3d6 | |||
| 140edc1cb1 | |||
| 56a910d714 | |||
| fc3a39b0b9 | |||
| 47c313a26a | |||
| 5332818990 | |||
| 0119923ed7 | |||
| 4a8fff4384 | |||
| 0ce3ebdb35 | |||
| 130e346319 | |||
| 5003661a63 | |||
| 043bca95a2 | |||
| 6bd82a2a91 | |||
| 2a21a23462 |
@@ -71,3 +71,5 @@ 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
|
||||||
+1
-1
@@ -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.net</DnsSuffix>
|
<DnsSuffix>.internal.mydomain.tld</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"/>
|
||||||
|
|||||||
+136
-22
@@ -1,19 +1,37 @@
|
|||||||
<#
|
<#
|
||||||
#>
|
#>
|
||||||
|
|
||||||
|
[CmdletBinding()]
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
# Computer to wake up
|
# Array of strings listing computer name(s) to wake up. Names will be matched using RegEx
|
||||||
[Alias("Computer")]
|
[Alias("Computer")]
|
||||||
|
[String[]]
|
||||||
|
$TargetComputer = "",
|
||||||
|
# XML database file containing computer details (Default: WOLDatabase.xml)
|
||||||
|
[Alias("List", "Database")]
|
||||||
[String]
|
[String]
|
||||||
$targetComputer = "",
|
$WolDatabase = "WOLDatabase.xml",
|
||||||
# Skip RDP connectivity readiness check
|
# Skip connectivity check
|
||||||
[Alias("Check", "Readiness")]
|
[Alias("NoCheck", "NoReadiness")]
|
||||||
[Switch]
|
[Switch]
|
||||||
$ConnectivityCheck = $false,
|
$NoConnectivityCheck,
|
||||||
# RDP connectivity readiness check timeout
|
# Delay before running first connectivity check (Default: 10)
|
||||||
|
[Alias("Delay")]
|
||||||
[Int]
|
[Int]
|
||||||
$Timeout = 300
|
$ConnectivityCheckDelay = 10,
|
||||||
|
# Seconds before aborting connectivity check (Default: 300)
|
||||||
|
[Alias("Timeout")]
|
||||||
|
[Int]
|
||||||
|
$ConnectivityCheckTimeout = 300,
|
||||||
|
# Seconds between connectivity checks (Default: 30)
|
||||||
|
[Alias("Interval")]
|
||||||
|
[Int]
|
||||||
|
$ConnectivityCheckInterval = 30,
|
||||||
|
# Port to use for connectivity check (Default: 3389-RDP)
|
||||||
|
[Alias("CheckPort")]
|
||||||
|
[Int]
|
||||||
|
$ConnectivityCheckPort = 3389
|
||||||
)
|
)
|
||||||
|
|
||||||
function exitError($errMessage, $PSItem, $exitCode = 1)
|
function exitError($errMessage, $PSItem, $exitCode = 1)
|
||||||
@@ -32,8 +50,14 @@ function exitError($errMessage, $PSItem, $exitCode = 1)
|
|||||||
Exit $exitCode
|
Exit $exitCode
|
||||||
}
|
}
|
||||||
|
|
||||||
$wolModuleName = "wol-magicPacket"
|
function exitGracefully()
|
||||||
$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))
|
||||||
@@ -50,31 +74,41 @@ 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]$wolDb = Get-Content -Path $WolDatabase
|
||||||
$broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress
|
$broadcastIP = $wolDb.WOLDatabase.Configuration.BroadcastAddress
|
||||||
$port = $db.WOLDatabase.Configuration.Port
|
$port = $wolDb.WOLDatabase.Configuration.Port
|
||||||
$dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix
|
$dnsSuffix = $wolDb.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($_) }
|
$TargetComputer | ForEach-Object {
|
||||||
|
$tgt = $_
|
||||||
|
$wolDb.WOLDatabase.Computers.Computer | Where-Object { $_.name -match $tgt } | 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 = @()
|
||||||
@@ -98,6 +132,7 @@ $targetComputers | ForEach-Object {
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Write-Host -ForegroundColor Red "[ERROR]"
|
Write-Host -ForegroundColor Red "[ERROR]"
|
||||||
|
# queue for removal from computers on which to run a connectivity check
|
||||||
$removeFromTargetComputers.Add($name)
|
$removeFromTargetComputers.Add($name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +143,86 @@ $removeFromTargetComputers | ForEach-Object {
|
|||||||
$targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null
|
$targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
# process RDP readiness check
|
# exit if connectivity check skipped or if no computers to test
|
||||||
$targetComputers
|
if ($NoConnectivityCheck)
|
||||||
|
{
|
||||||
|
Write-Host "`r`nSkipping connectivity checks."
|
||||||
|
exitGracefully
|
||||||
|
}
|
||||||
|
elseif ($targetComputers.Count -eq 0)
|
||||||
|
{
|
||||||
|
exitError "No wake-up packets sent successfully" -exitCode 5
|
||||||
|
}
|
||||||
|
|
||||||
Exit 0
|
# wait for initial delay seconds to let computer(s) wake up
|
||||||
|
for ($i = $ConnectivityCheckDelay; $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
|
||||||
|
# TODO: run connectivity test as background job
|
||||||
|
$connCheckTotalTime = 0
|
||||||
|
$removeFromTargetComputers.Clear()
|
||||||
|
do
|
||||||
|
{
|
||||||
|
$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 $ConnectivityCheckPort -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable +connectionError))
|
||||||
|
{
|
||||||
|
if (($connCheckTotalTime + $ConnectivityCheckInterval) -gt $ConnectivityCheckTimeout)
|
||||||
|
{
|
||||||
|
# last iteration, display 'error'
|
||||||
|
Write-Host -ForegroundColor Red "[ERROR]"
|
||||||
|
$connectionError | ForEach-Object {
|
||||||
|
$errMsg = $_.ToString()
|
||||||
|
Write-Verbose "`t(Additional information: $errMsg)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
# more iterations pending, display 'retrying'
|
||||||
|
Write-Host -ForegroundColor Yellow "[RETRYING]"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Write-Host -ForegroundColor Green "[OK]"
|
||||||
|
# queue for removal from computers to check next round
|
||||||
|
$removeFromTargetComputers.Add($name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# remove computers from targetComputers if connectivity check already successful
|
||||||
|
$removeFromTargetComputers | ForEach-Object {
|
||||||
|
$removeName = $_
|
||||||
|
$targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null
|
||||||
|
}
|
||||||
|
|
||||||
|
# sleep until next test interval and increase time counter
|
||||||
|
$connCheckTotalTime += $ConnectivityCheckInterval
|
||||||
|
if ($connCheckTotalTime -le $ConnectivityCheckTimeout -and $targetComputers.Count -gt 0)
|
||||||
|
{
|
||||||
|
for ($i = $ConnectivityCheckInterval; $i -gt 0; $i--) {
|
||||||
|
Write-Progress -Activity "Waiting for next connectivity check..." -SecondsRemaining $i
|
||||||
|
Start-Sleep 1
|
||||||
|
}
|
||||||
|
Write-Progress -Activity "Waiting for next connectivity check..." -Completed
|
||||||
|
}
|
||||||
|
} while ($connCheckTotalTime -le $ConnectivityCheckTimeout -and $targetComputers.Count -gt 0)
|
||||||
|
|
||||||
|
# exit gracefully
|
||||||
|
exitGracefully
|
||||||
Reference in New Issue
Block a user