diff --git a/wakeup.ps1 b/wakeup.ps1 index 89139ad..9518a1e 100644 --- a/wakeup.ps1 +++ b/wakeup.ps1 @@ -14,10 +14,17 @@ param # Skip connectivity readiness check [Alias("NoCheck", "NoReadiness")] [Switch] - $ConnectivityCheck = $false, -# RDP connectivity readiness check timeout + $NoConnectivityCheck = $false, +# Initial wait period in seconds before first connectivity check [Int] - $Timeout = 300 + $WaitBeforeConnectivityCheck = 10, +# connectivity readiness check total timeout + [Int] + $Timeout = 300, +# connectivity readiness port to check + [Alias("CheckPort")] + [Int] + $ConnectivityPort = 3389 ) function exitError($errMessage, $PSItem, $exitCode = 1) @@ -125,7 +132,49 @@ $removeFromTargetComputers | ForEach-Object { $targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null } -# process RDP readiness check -$targetComputers +# exit if connectivity check skipped or if no computers to test +if ($NoConnectivityCheck) +{ + Write-Host "`r`nSkipping connectivity checks." + exitGracefully +} +elseif ($targetComputers.Count -eq 0) +{ + exitError "No successful wake-up packets sent" -exitCode 5 +} -Exit 0 \ No newline at end of file +# wait for initialWaitTime seconds to let computer(s) wake up +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 \ No newline at end of file