How can I wrap this Powershell cmdlet into a timeout function?

与世无争的帅哥 提交于 2021-01-29 07:00:44

问题


I've tried (and failed) to get [System.Delegate]::CreateDelegate( to work, New-Object [System.Threading.Tasks.Task]::Run(, to work, Start-Job with Wait-Job, Powershell does not seem to be setup to do an asynchronous task with a wait timeout?

If anyone has any ideas, that'd be awesome.

do 
{
    Sleep 2;
    $testShare = Test-Path "\\$server\c$";
    Write-Host "Share availability is:" $testShare;
}while($testShare -eq $true) # wait for the share to become unavailable

Adams suggestion

do 
{
    Sleep 1;
    #$testShare = Test-Path "\\$server\c$"; # could use code here to deal with the hanging

    $timeout_in_seconds = 5;
    $timer = [Diagnostics.Stopwatch]::StartNew();
    do 
    {
        Write-Host "    Test-path inside of second do loop";
        Start-Sleep -Seconds 1;
        $testShare = Test-Path "\\$server\c$";
        Write-Host "    (Inner loop) Share availability is:" $testShare;
    } while ( (1 -eq 1) -and ($timer.Elapsed.TotalSeconds -lt $timeout_in_seconds) )
    $timer.Stop();
    $timer.Elapsed.TotalSeconds;

    Write-Host "";
    Write-Host "(Outer loop) Share availability is:" $testShare;
} while($testShare -eq $true) # wait for the share to become unavailable

Output

    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
5.3015436

(Outer loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
5.2303907

(Outer loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: False
**42.1773323**

(Outer loop) Share availability is: False
Ping availability is: False

回答1:


A couple things...

Having a timeout doesn't necessitate async processing. You could have a sync process (like your example) that has a timeout.

Simple sync script with a timeout...

$timeout_in_seconds = 10
$timer = [Diagnostics.Stopwatch]::StartNew()

do {
    Start-Sleep -Seconds 1
    Write-Host 'Doing stuff'

} while ( (1 -eq 1) -and ($timer.Elapsed.TotalSeconds -lt $timeout_in_seconds) )

$timer.Stop()
$timer.Elapsed.TotalSeconds

Simplified your example just a hair to demonstrate the point. I'm setting a run interval (10 sec). I'm firing up a timer. I run a loop until I've hit a success condition (which, in this example, I never will) or I hit the timeout. You'd do the same thing.

For your specific example, consider something like...

$server = '...'
$timeout_in_seconds = 5;
$timer = [Diagnostics.Stopwatch]::StartNew();

do {
    Write-Host "Test-path inside of loop";
    Start-Sleep -Seconds 1;
    $testShare = Test-Path "\\$server\c$";
    Write-Host "Share availability is:" $testShare;
} while ( $($testShare) -and ($timer.Elapsed.TotalSeconds -lt $timeout_in_seconds) )

$timer.Stop();
$timer.Elapsed.TotalSeconds;

The loop terminates once the share exists or the time interval is reached. Note, you'll need to set your $server variable.



来源:https://stackoverflow.com/questions/56087602/how-can-i-wrap-this-powershell-cmdlet-into-a-timeout-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!