gpt4 book ai didi

powershell - 停止 PowerShell 管道,确保调用 end

转载 作者:行者123 更新时间:2023-12-04 02:26:14 25 4
gpt4 key购买 nike

我想要做的是获得一个功能来在达到时间限制时停止管道馈送。我创建了一个测试函数,如下所示:

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{}”块,这在这种情况下至关重要。有谁知道为什么当我使用“continue”停止管道时我的“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子句实际处理仅在达到时间限制之前进行。

关于powershell - 停止 PowerShell 管道,确保调用 end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30626533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com