MOL: Learn PowerShell Scripting (LiveBook) Ch15. Error

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'

Thank you - we’ll try and get that information to the publisher.