PowerShell:打破嵌套循环

go

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

$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

我做错了什么?

请给我goto和标签吗?

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

用户2555451

break与循环标签一起使用时请勿添加冒号这行:

break :outer

应该这样写:

break outer

为了进一步演示,请考虑以下简单脚本:

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

执行后,它将永久运行而不会中断。但是此脚本:

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

退出,因为我更改break :loopbreak loop

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章