cmdlet包装到超时函数中

cmdlet包装到超时函数中

本文介绍了如何将此Powershell cmdlet包装到超时函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过(但失败了)让[System.Delegate] :: CreateDelegate(工作,新对象[System.Threading.Tasks.Task] :: Run(工作,等待带有Start-Job-Job,似乎没有设置Powershell来执行带有等待超时的异步任务吗?

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

亚当斯的建议

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

输出

    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

推荐答案

几件事...

发生超时并不需要异步处理.您可能有一个具有超时的同步过程(例如您的示例).

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

简化示例只是为了说明这一点.我正在设置运行间隔(10秒).我正在启动一个计时器.我运行一个循环,直到遇到成功条件(在此示例中,我永远不会成功)或达到超时.您会做同样的事情.

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;

一旦共享存在或达到时间间隔,循环就会终止.请注意,您需要设置 $ server 变量.

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

这篇关于如何将此Powershell cmdlet包装到超时函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:49