refactor(wakeup): rework params, conn-check multiple err msg

- rename connectivity check parameters for consistency
- allow multiple errors to be reported for connectivity check for
troubleshooting
This commit is contained in:
Asif Bacchus 2021-09-06 15:16:21 -06:00
parent 4a8fff4384
commit 0119923ed7

View File

@ -7,24 +7,30 @@ param
[Alias("Computer")] [Alias("Computer")]
[String[]] [String[]]
$TargetComputer = "", $TargetComputer = "",
# XML database file containing computer details # XML database file containing computer details (Default: WOLDatabase.xml)
[Alias("List", "database", "db")] [Alias("List", "database", "db")]
[String] [String]
$WolDatabase = "WOLDatabase.xml", $WolDatabase = "WOLDatabase.xml",
# Skip connectivity readiness check # Skip connectivity check
[Alias("NoCheck", "NoReadiness")] [Alias("NoCheck", "NoReadiness")]
[Switch] [Switch]
$NoConnectivityCheck = $false, $NoConnectivityCheck,
# Initial wait period in seconds before first connectivity check # Delay before running first connectivity check (Default: 10)
[Alias("Delay")]
[Int] [Int]
$WaitBeforeConnectivityCheck = 10, $ConnectivityCheckDelay = 10,
# connectivity readiness check total timeout # Seconds before aborting connectivity check (Default: 300)
[Alias("Timeout")]
[Int] [Int]
$Timeout = 300, $ConnectivityCheckTimeout = 300,
# connectivity readiness port to check # Seconds between connectivity checks (Default: 30)
[Alias("Interval")]
[Int]
$ConnectivityCheckInterval = 30,
# Port to use for connectivity check (Default: 3389-RDP)
[Alias("CheckPort")] [Alias("CheckPort")]
[Int] [Int]
$ConnectivityPort = 3389 $ConnectivityCheckPort = 3389
) )
function exitError($errMessage, $PSItem, $exitCode = 1) function exitError($errMessage, $PSItem, $exitCode = 1)
@ -143,22 +149,26 @@ if ($NoConnectivityCheck)
} }
elseif ($targetComputers.Count -eq 0) elseif ($targetComputers.Count -eq 0)
{ {
exitError "No successful wake-up packets sent" -exitCode 5 exitError "No wake-up packets sent successfully" -exitCode 5
} }
# wait for initialWaitTime seconds to let computer(s) wake up # wait for initialWaitTime seconds to let computer(s) wake up
for ($i = $WaitBeforeConnectivityCheck; $i -gt 0; $i--) { for ($i = $ConnectivityCheckDelay; $i -gt 0; $i--) {
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -SecondsRemaining $i Write-Progress -Activity "Waiting for computer(s) to wake-up..." -SecondsRemaining $i
Start-Sleep 1 Start-Sleep 1
} }
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -Completed Write-Progress -Activity "Waiting for computer(s) to wake-up..." -Completed
# iterate computers and test connectivity # iterate computers and test connectivity
# TODO: run multiple connectivity tests as needed, spaced evenly apart up to max timeout THEN fail
# TODO: run connectivity test as background job
$targetComputers | ForEach-Object { $targetComputers | ForEach-Object {
$name = $_.name $name = $_.name
$friendlyName = $_.friendlyName $friendlyName = $_.friendlyName
$fqdn = -join ($name, $dnsSuffix) $fqdn = -join ($name, $dnsSuffix)
$connectionError = "" $connectionError = @()
if ( [String]::IsNullOrWhiteSpace($friendlyName)) if ( [String]::IsNullOrWhiteSpace($friendlyName))
{ {
@ -168,10 +178,13 @@ $targetComputers | ForEach-Object {
{ {
Write-Host "Testing connection readiness of '$friendlyName'... " -NoNewline Write-Host "Testing connection readiness of '$friendlyName'... " -NoNewline
} }
if (!(Test-NetConnection -ComputerName $fqdn -Port $ConnectivityPort -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable connectionError)) if (!(Test-NetConnection -ComputerName $fqdn -Port $ConnectivityCheckPort -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable +connectionError))
{ {
Write-Host -ForegroundColor Red "[ERROR]" Write-Host -ForegroundColor Red "[ERROR]"
Write-Host -ForegroundColor Red "`tAdditional information:", $connectionError[-1].ToString() $connectionError | ForEach-Object {
$errMsg = $_.ToString()
Write-Host -ForegroundColor Red "`tAdditional information: $errMsg"
}
} }
else else
{ {