feature(wakeup): run multiple connectivity checks up to timeout

This commit is contained in:
Asif Bacchus 2021-09-06 15:38:36 -06:00
parent 0119923ed7
commit 5332818990

View File

@ -131,6 +131,7 @@ $targetComputers | ForEach-Object {
else
{
Write-Host -ForegroundColor Red "[ERROR]"
# queue for removal from computers on which to run a connectivity check
$removeFromTargetComputers.Add($name)
}
}
@ -152,7 +153,7 @@ elseif ($targetComputers.Count -eq 0)
exitError "No wake-up packets sent successfully" -exitCode 5
}
# wait for initialWaitTime seconds to let computer(s) wake up
# 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
@ -162,33 +163,55 @@ Write-Progress -Activity "Waiting for computer(s) to wake-up..." -Completed
# 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
$connCheckTotalTime = 0
$removeFromTargetComputers.Clear()
while ($connCheckTotalTime -le $ConnectivityCheckTimeout -and $targetComputers.Count -gt 0)
{
$targetComputers | ForEach-Object {
$name = $_.name
$friendlyName = $_.friendlyName
$fqdn = -join ($name, $dnsSuffix)
$connectionError = @()
$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))
{
Write-Host -ForegroundColor Red "[ERROR]"
$connectionError | ForEach-Object {
$errMsg = $_.ToString()
Write-Host -ForegroundColor Red "`tAdditional information: $errMsg"
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))
{
Write-Host -ForegroundColor Red "[ERROR]"
$connectionError | ForEach-Object {
$errMsg = $_.ToString()
Write-Host -ForegroundColor Red "`tAdditional information: $errMsg"
}
}
else
{
Write-Host -ForegroundColor Green "[OK]"
# queue for removal from computers to check next round
$removeFromTargetComputers.Add($name)
}
}
else
# 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
if ($targetComputers.Count -gt 0)
{
Write-Host -ForegroundColor Green "[OK]"
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
$connCheckTotalTime += $ConnectivityCheckInterval
}
}