Test-Path timeout for PowerShell - Stack Overflow

PHOTO EMBED

Tue Jul 07 2020 08:31:09 GMT+0000 (Coordinated Universal Time)

Saved by @peota #bsh

$sleepDuration = Get-Random 2,3
$ps = [powershell]::Create().AddScript("Start-Sleep -Seconds $sleepDuration; 'Done!'")

# execute it asynchronously
$handle = $ps.BeginInvoke()

# Wait 2500 milliseconds for it to finish
if(-not $handle.AsyncWaitHandle.WaitOne(2500)){
    throw "timed out"
    return
}

# WaitOne() returned $true, let's fetch the result
$result = $ps.EndInvoke($handle)

return $result
content_copyCOPY

https://stackoverflow.com/questions/60676732/test-path-timeout-for-powershell