停止PowerShell管道,确保结束被调用

前端之家收集整理的这篇文章主要介绍了停止PowerShell管道,确保结束被调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要做的是获得一个功能,以便在达到时间限制时停止管道馈送.我创建了一个测试函数,如下所示:
function Test-PipelineStuff
{
    [cmdletbinding()]
    Param(
        [Parameter(ValueFromPipeLIne=$true)][int]$Foo,[Parameter(ValueFromPipeLIne=$true)][int]$MaxMins
    )

    begin { 
        "THE START" 
        $StartTime = Get-Date
        $StopTime = (get-date).AddMinutes($MaxMins)
        "Stop time is: $StopTime"
    } 

    process 
    {  
        $currTime = Get-Date
        if( $currTime -lt $StopTime ){
            "Processing $Foo"            
        }
        else{
            continue;
        }
    }

    end { "THE END" }
}
@H_502_3@这一定会阻止管道,但是它从不称为“end {}”块,在这种情况下,这个块是至关重要的.有人知道为什么当我停止使用“继续”的管道时,我的“end {}”块不被调用?如果我抛出一个PipelineStoppedException,行为似乎是一样的.

根据 about_Functions
@H_502_3@After the function receives all the objects in the pipeline,the End
statement list runs one time. If no Begin,Process,or End keywords
are used,all the statements are treated like an End statement list.

@H_502_3@因此,你只需要省略else块.然后管道中的所有对象都被处理,但是由于if子句,实际的处理只有在时间限制被触发之前才进行处理.

原文链接:https://www.f2er.com/javaschema/281431.html

猜你在找的设计模式相关文章