Retry only transient outcomes
Add a small, explicit retry budget without repeating successful, refused, or invalid probes.
Retries change the cost of a plan
The scanner makes one attempt per endpoint. A retry wrapper can improve a transient observation, but it multiplies work. This exercise allows one to three total attempts and retries only TimedOut. Open, Refused, and Error end the loop immediately.
MaxAttempts includes the first attempt. With MaxAttempts 2, an initial timeout permits one additional probe, not two. Always pass the same per-attempt timeout to the adapter. The maximum planned wait is attempt count times per-attempt timeout, plus execution overhead; it is not a strict wall-clock guarantee.
Preserve the cost and the final observation
Return Attempts and TotalElapsedMs, adding the durations reported by each attempt. Status and Detail describe the final observation. If every attempt times out, keep TimedOut. Do not turn an exhausted retry budget into Refused or a success.
The supplied PortScanner.ps1 already validates adapter results and turns thrown errors into Error rows. Reuse it for one endpoint per attempt. This helper assumes its caller chose the authorised scope; it does not expand targets.
Keep tests deterministic
A test probe can count calls and return TimedOut first, then Open. Another can always time out. Neither requires real sleeping. Production retry policies may need backoff, jitter, cancellation, and a shared overall deadline; those are outside this deliberately sequential browser exercise. The lab's execution deadline still applies to the whole script.
$attempts=2; $timeoutMs=200
[pscustomobject]@{MaximumPlannedWaitMs=$attempts*$timeoutMs}
Put it into practice.
Using the supplied scanner, define Invoke-RetryProbe(ComputerName, Port, Probe, TimeoutMs=200, MaxAttempts=2). Validate MaxAttempts 1–3 and TimeoutMs 1–5000. Retry only TimedOut, up to MaxAttempts total calls. Return ComputerName, Port, final Status/Detail, Attempts, and summed TotalElapsedMs.
See an explained solution
. '/home/user/ScanPlan.ps1'
. '/home/user/PortScanner.ps1'
function Invoke-RetryProbe {
param([string]$ComputerName,[int]$Port,[scriptblock]$Probe,
[ValidateRange(1,5000)][int]$TimeoutMs=200,[ValidateRange(1,3)][int]$MaxAttempts=2)
$attempts=0; $total=0
do {
$result=Invoke-PortScan @($ComputerName) @($Port) @($ComputerName) $Probe $TimeoutMs
$attempts++; $total+=$result.ElapsedMs
} while($result.Status -eq 'TimedOut' -and $attempts -lt $MaxAttempts)
[pscustomobject]@{ComputerName=$ComputerName;Port=$Port;Status=$result.Status;Attempts=$attempts;TotalElapsedMs=$total;Detail=$result.Detail}
}
Only the documented transient category consumes another attempt. The result exposes the actual call count and cumulative measured duration.