Using break or return inside of a invoke-command.

Welcome Forums General PowerShell Q&A Using break or return inside of a invoke-command.

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 year, 9 months ago.

  • Author
    Posts
  • #65833

    Participant
    Points: 1
    Rank: Member

    Hi all

    I have a nested look, that I need to break, when a Array List turns to 0. This works just fine when i'm running it locally.. But when I use it remote, in invoke-command, it breaks out of both loops and the PSsession.

    Can some one help me, figure out what i'm doing wrong here. I thourd that using

    :inner

    and then

    break inner

    would do the trick as I'm telling powershell to break out of the nested loop, and continuing on, in the top loop.

    do
    {
    # The :inner is used to break out of the nested look below here.
    :inner
    
        $CurrentNumber = $Counter
        $StartCount++
    
        do
        {
            $MergedList1 += $TotalMergedListUnique[0]
            $TotalMergedListUnique.Remove($TotalMergedListUnique[0])
    
            if ($TotalMergedListUnique.count -eq 0)
            {
                break inner
            }
    
        }
        until ( ($MergedList1.count -eq 200) )
    
        $($MergedList1 -join " | ")
        " `n `n  "
        $Counter++
    }
    until ($Counter -eq $GroupCount)
    

    Please do tell me if I'm missing out on some critical information to answer this question.

  • #65842

    Participant
    Points: 0
    Rank: Member

    regardless of the 'break' why you doesn't use until ((TotalMergedListUnique.count -eq 0) -or ($MergedList1.count -eq 200) ?

    the help states that "A Break statement can include a label. If you use the Break keyword with
    a label, Windows PowerShell exits the labeled loop instead of exiting the
    current loop."
    and your label not before the loop as showed in example
    "it must be followed by the looping keyword, such as While."

  • #65868

    Participant
    Points: 91
    Rank: Member

    Daniel,

    Can you provide sample input such as

    $TotalMergedListUnique = @(1,2,3,4,5,6,7,8,9,10)
    $startCount =...
    

    and desired output?

  • #66010

    Participant
    Points: 1
    Rank: Member

    @Max Kozlov: I do think only god can tell why I didn't use

    until ((TotalMergedListUnique.count -eq 0) -or ($MergedList1.count -eq 200)

    it do what I need without Return or/and Break... Same sec I read your comment, I was like.. What the Bl*** H***! 🙂
    Thank you so much for that note... Some times you can see the forest for all the trees...

    @Sam Boutros:
    $TotalMergedListUnique = @("*.fileex0","*.fileex2","*.fileex3","*.fileex4","*.fileex5",)
    $startCount = 0

    But Max solved for me... I've just been away for my PC to update the post here.

    Thanks both of you, for taking your time to reply on my post 🙂

The topic ‘Using break or return inside of a invoke-command.’ is closed to new replies.