gpt4 book ai didi

loops - PowerShell:打破嵌套循环

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

应该有 break PowerShell 中的命令,可以通过分配标签退出嵌套循环。只是它不起作用。这是我的代码:

$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
}

它打印以下内容:
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

我做错了什么?

请给我转到和标签好吗?

使用 Windows 7,我猜是 PS 2.0。这个脚本至少应该在 PS 2 上运行。

最佳答案

使用 break 时不要添加冒号带有循环标签。这一行:

break :outer

应该这样写:

break outer

为了进一步演示,请考虑这个简单的脚本:

:loop while ($true)
{
while ($true)
{
break :loop
}
}

执行时,它将永远运行而不会中断。然而这个脚本:

:loop while ($true)
{
while ($true)
{
break loop
}
}

退出,因为我改变了 break :loopbreak loop .

关于loops - PowerShell:打破嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043990/

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