MOL: Learn PowerShell Scripting (LiveBook) Ch15. Error

Welcome Forums General PowerShell Q&A MOL: Learn PowerShell Scripting (LiveBook) Ch15. Error

This topic contains 1 reply, has 2 voices, and was last updated by

1 week, 2 days ago.

  • Author
  • #113810

    Points: 0
    Rank: Member

    Noticed what appears to be an error in the MOL Livebook:

    If you copy and run this code as is, the Do Loop results in an infinite loop due to the placement of the variable $protocol. For example, if there is an error, the code will move to the Catch block change the $protocol variable, but then change right back to the original protocol. Further, it appears to me that this code on success will be stuck in an infinite loop as well, since the Do Loop has no way of completing the Until condition within the Try block.

    Ch15. Listing 15.2 "Our Take":


    One way to fix the infinite loops might be adjust the code as follows:

    Move the initial $protocol variable above the Do loop.

    Process {
         foreach ($Computer in $ComputerName) {
         $protocol = 'Wsman'
             Do {
               Try {

    and create exit condition in the Try block

    $session | Remove-CimSession
    #Exit condition of Do loop
    $protocol = 'Stop'
  • #113819

    Points: 1
    Rank: Member

    Thank you – we'll try and get that information to the publisher.

You must be logged in to reply to this topic.