[英]PowerShell: break nested loops
There should be a break
command in PowerShell that can exit nested loops by assigning a label. PowerShell中应该有一个
break
命令,可以通过分配标签来退出嵌套循环。 Just it doesn't work. 只是它不起作用。 Here's my code:
这是我的代码:
$timestampServers = @(
"http://timestamp.verisign.com/scripts/timstamp.dll",
"http://timestamp.comodoca.com/authenticode",
"http://timestamp.globalsign.com/scripts/timstamp.dll",
"http://www.startssl.com/timestamp"
)
:outer for ($retry = 2; $retry -gt 0; $retry--)
{
Write-Host retry $retry
foreach ($timestampServer in $timestampServers)
{
Write-Host timestampServer $timestampServer
& $signtoolBin sign /f $keyFile /p "$password" /t $timestampServer $file
if ($?)
{
Write-Host OK
break :outer
}
}
}
if ($retry -eq 0)
{
WaitError "Digitally signing failed"
exit 1
}
It prints the following: 它打印以下内容:
retry 2
timestampServer http://timestamp.verisign.com/scripts/timstamp.dll
Done Adding Additional Store
Successfully signed and timestamped: C:\myfile.dll
OK
retry 1
timestampServer http://timestamp.verisign.com/scripts/timstamp.dll
Done Adding Additional Store
Successfully signed and timestamped: C:\myfile.dll
OK
ERROR: Digitally signing failed
What have I done wrong? 我做错了什么?
Can I have goto and labels, please? 我可以带goto和标签吗?
Using Windows 7 and I guess PS 2.0. 使用Windows 7,我猜PS 2.0。 This script is supposed to run on PS 2 at least.
该脚本应该至少在PS 2上运行。
You do not add the colon when using break
with a loop label. 使用带有循环标签的
break
时,不要添加冒号。 This line: 这一行:
break :outer
should be written like this: 应该这样写:
break outer
For a further demonstration, consider this simple script: 有关进一步演示,请考虑以下简单脚本:
:loop while ($true)
{
while ($true)
{
break :loop
}
}
When executed, it will run forever without breaking. 执行时,它将永远运行而不会中断。 This script however:
但是这个脚本:
:loop while ($true)
{
while ($true)
{
break loop
}
}
exits as it should because I changed break :loop
to break loop
. 应该退出,因为我改变了
break :loop
来break loop
。
So, I changed the code a bit to make it clear 所以,我稍微更改了代码以使其清楚
$timestampServers = @(
"http://timestamp.verisign.com/scripts/timstamp.dll",
"http://timestamp.comodoca.com/authenticode",
"http://timestamp.globalsign.com/scripts/timstamp.dll",
"http://www.startssl.com/timestamp"
)
:outer for ($retry = 2; $retry -gt 0; $retry--)
{
Write-Host retry $retry
foreach ($timestampServer in $timestampServers)
{
Write-Host timestampServer $timestampServer
#& $signtoolBin sign /f $keyFile /p "$password" /t $timestampServer $file
if ($true)
{
break :outer
Write-Host OK
}
}
}
if ($retry -eq 0)
{
Write-Error "Digitally signing failed" ## you have a typo there
exit 1
}
This produces the following: 这产生以下结果:
retry 2
timestampServer http://timestamp.verisign.com/scripts/timstamp.dll
retry 1
timestampServer http://timestamp.verisign.com/scripts/timstamp.dll
C:\temp\t.ps1 : Digitally signing failed
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,t.ps1
So, skips Write-Host OK, but also seems to continue to loop. 所以,跳过Write-Host OK,但似乎还在继续循环。 In other words, it acts like 'Continue' statement.
换句话说,它的行为类似于“继续”声明。
Changed it like the folks mentioned to remove ':', although PowerShell documentation does not exclude it: 虽然PowerShell文档没有排除它,但改变它就像提到删除':'的人一样:
if ($true)
{
break outer
Write-Host OK
}
I get the correct behavior. 我得到了正确的行为。
retry 2
timestampServer http://timestamp.verisign.com/scripts/timstamp.dll
Long story short... do not use ':' 长话短说...不要使用':'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.