PowerShell: breaking nested loops

PowerShell must have a command breakthat can exit nested loops by assigning a label. It just doesn't work. Here is 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
}

He 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 did I do wrong?

Can I get tags, please?

Using Windows 7 and, I think, PS 2.0. This script is supposed to work on PS 2 at least.

+4
source share
2 answers

You do not add a colon when used breakwith a loop label. This line:

break :outer

should be written like this:

break outer

For an additional demonstration, consider this simple script:

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

When executed, it will work forever without breaking. However, this script:

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

, break :loop break loop.

+14

, , .

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

:

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

, Write-Host OK, , , . , "".

, , ':', PowerShell :

 if ($true)
        {

            break outer
            Write-Host OK
        }

.

retry 2
timestampServer http://timestamp.verisign.com/scripts/timstamp.dll

... ':'

+1

Source: https://habr.com/ru/post/1543252/


All Articles