加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 20:25:02 所属栏目:百科 来源:网络整理
导读:我想要做的是获得一个功能,以便在达到时间限制时停止管道馈送.我创建了一个测试函数,如下所示: function Test-PipelineStuff{ [cmdletbinding()] Param( [Parameter(ValueFromPipeLIne=$true)][int]$Foo,[Parameter(ValueFromPipeLIne=$true)][int]$MaxMins
我想要做的是获得一个功能,以便在达到时间限制时停止管道馈送.我创建了一个测试函数,如下所示:
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" }
}

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

根据 about_Functions:

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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读