ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Powershell countdown on servers?

    IT Discussion
    6
    19
    607
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • RomoR
      Romo @Grey
      last edited by Romo

      @Grey $quantity inside the loop will never change to any other value other than $quantity - 1. So if quantity is originally defined as 5 you $newquant will always be 4, no matter how many times your loop runs as you currently have it now.

      As @JaredBusch mentions you need to decrement $quantity to actually have a counter.

      1 Reply Last reply Reply Quote 0
      • dafyreD
        dafyre @Grey
        last edited by

        @Grey said in Powershell countdown on servers?:

        If I have a foreach running on 20 servers, and I want to display a countdown for the run, decrementing the count each time, how would that work? I have:

        $quantity = $systems.count
        foreach ($server in $systems){
        //stuff
        $newquant = $quantity-1
        }
        

        This always returns a fixed value, though.

        If youw ant to do it absolutely with $newquantity, then do this...

        $quantity = $systems.count
        $newquantity = $quantity
        
        foreach ($server in $systems){
        //stuff
        $newquantity = $newquantity-1
        }
        
        GreyG 1 Reply Last reply Reply Quote 0
        • GreyG
          Grey @dafyre
          last edited by

          @dafyre said in Powershell countdown on servers?:

          @Grey said in Powershell countdown on servers?:

          If I have a foreach running on 20 servers, and I want to display a countdown for the run, decrementing the count each time, how would that work? I have:

          $quantity = $systems.count
          foreach ($server in $systems){
          //stuff
          $newquant = $quantity-1
          }
          

          This always returns a fixed value, though.

          If youw ant to do it absolutely with $newquantity, then do this...

          $quantity = $systems.count
          $newquantity = $quantity
          
          foreach ($server in $systems){
          //stuff
          $newquantity = $newquantity-1
          }
          

          Testing this now.

          1 Reply Last reply Reply Quote 0
          • dafyreD
            dafyre
            last edited by

            Are you doing this so you can have a progress bar or what?

            GreyG 1 Reply Last reply Reply Quote 0
            • GreyG
              Grey @dafyre
              last edited by

              @dafyre said in Powershell countdown on servers?:

              Are you doing this so you can have a progress bar or what?

              Positive feedback on a script that's iterating actions on a group of servers, so... kind of a progress bar.

              dafyreD 1 Reply Last reply Reply Quote 0
              • dafyreD
                dafyre @Grey
                last edited by

                @Grey said in Powershell countdown on servers?:

                @dafyre said in Powershell countdown on servers?:

                Are you doing this so you can have a progress bar or what?

                Positive feedback on a script that's iterating actions on a group of servers, so... kind of a progress bar.

                if ($newquantity -eq 0) {
                 write-host "We're done, everybody go home!"
                }
                
                GreyG 1 Reply Last reply Reply Quote 0
                • GreyG
                  Grey @dafyre
                  last edited by

                  @dafyre said in Powershell countdown on servers?:

                  @Grey said in Powershell countdown on servers?:

                  @dafyre said in Powershell countdown on servers?:

                  Are you doing this so you can have a progress bar or what?

                  Positive feedback on a script that's iterating actions on a group of servers, so... kind of a progress bar.

                  if ($newquantity -eq 0) {
                   write-host "We're done, everybody go home!"
                  }
                  

                  Oh, fun! I hadn't considered the end. I'm more of a Tron guy so ... End of Line.

                  1 Reply Last reply Reply Quote 1
                  • ObsolesceO
                    Obsolesce
                    last edited by

                    You need to tell it to subtract first:

                    $quantity = $systems.Count
                    
                    foreach ($server in $systems) {
                    
                        Write-Output "Quantity is: [$quantity]"
                        $quantity--
                        $newquant = $quantity
                        Write-Output "`tNEW quantity is: [$newquant]"
                    
                    }
                    
                    JaredBuschJ 1 Reply Last reply Reply Quote 0
                    • JaredBuschJ
                      JaredBusch @Obsolesce
                      last edited by

                      @Obsolesce said in Powershell countdown on servers?:

                      You need to tell it to subtract first:

                      $quantity = $systems.Count
                      
                      foreach ($server in $systems) {
                      
                          Write-Output "Quantity is: [$quantity]"
                          $quantity--
                          $newquant = $quantity
                          Write-Output "`tNEW quantity is: [$newquant]"
                      
                      }
                      

                      The point is there is never a need for $newquantity

                      ObsolesceO 1 Reply Last reply Reply Quote 0
                      • ObsolesceO
                        Obsolesce @JaredBusch
                        last edited by

                        @JaredBusch said in Powershell countdown on servers?:

                        @Obsolesce said in Powershell countdown on servers?:

                        You need to tell it to subtract first:

                        $quantity = $systems.Count
                        
                        foreach ($server in $systems) {
                        
                            Write-Output "Quantity is: [$quantity]"
                            $quantity--
                            $newquant = $quantity
                            Write-Output "`tNEW quantity is: [$newquant]"
                        
                        }
                        

                        The point is there is never a need for $newquantity

                        I gathered he knows that, but still wants it in there.

                        1 Reply Last reply Reply Quote 0
                        • ObsolesceO
                          Obsolesce @Grey
                          last edited by

                          @Grey said in Powershell countdown on servers?:

                          $newquant = $quantity-1
                          

                          This always returns a fixed value, though.

                          Do it like this:

                          $newquant = --$quantity
                          
                          1 Reply Last reply Reply Quote 0
                          • 1
                            1337
                            last edited by 1337

                            @Grey

                            If you wanted to show the progress % you could do something like this as well:

                            $quantity = $systems.count
                            $i = 0
                            
                            foreach ($server in $systems){
                               //stuff
                               $i = $i + 1
                               $progress = 100 * $i / $quantity
                            }
                            

                            Another trick is to not count it at all, just print a dot (".") for each iteration.
                            .........

                            GreyG 1 Reply Last reply Reply Quote 2
                            • GreyG
                              Grey @1337
                              last edited by

                              @Pete-S said in Powershell countdown on servers?:

                              @Grey

                              If you wanted to show the progress % you could do something like this as well:

                              $quantity = $systems.count
                              $i = 0
                              
                              foreach ($server in $systems){
                                 //stuff
                                 $i = $i + 1
                                 $progress = 100 * $i / $quantity
                              }
                              

                              Another trick is to not count it at all, just print a dot (".") for each iteration.
                              .........

                              Tested and this needs a try/catch for a divide by zero. The math is also off.
                              9d767202-8760-4564-a5fa-0f1adc903aea-image.png

                              ObsolesceO 1 2 Replies Last reply Reply Quote 0
                              • ObsolesceO
                                Obsolesce @Grey
                                last edited by Obsolesce

                                @Grey said in Powershell countdown on servers?:

                                @Pete-S said in Powershell countdown on servers?:

                                @Grey

                                If you wanted to show the progress % you could do something like this as well:

                                $quantity = $systems.count
                                $i = 0
                                
                                foreach ($server in $systems){
                                   //stuff
                                   $i = $i + 1
                                   $progress = 100 * $i / $quantity
                                }
                                

                                Another trick is to not count it at all, just print a dot (".") for each iteration.
                                .........

                                Tested and this needs a try/catch for a divide by zero. The math is also off.
                                9d767202-8760-4564-a5fa-0f1adc903aea-image.png

                                This is how I would do the progress bar:

                                $systems = Get-Process #for my testing purposes
                                
                                $quantity = $systems.Count
                                $progressCount = 0
                                foreach ($server in $systems) {
                                    $progressCount++
                                    Write-Progress -Activity "Processing..." -Status "System: $($server.Name)" -PercentComplete ($progressCount/$systems.Count*100)
                                    $newquant = --$quantity
                                    Start-SLeep -Milliseconds 20 #added for testing so I can see the progress bar
                                }
                                

                                In my example, I used $systems = Get-Process for testing.

                                Edit: Change $($server.Name) to the property or info you want.

                                1 Reply Last reply Reply Quote 2
                                • 1
                                  1337 @Grey
                                  last edited by 1337

                                  @Grey said in Powershell countdown on servers?:

                                  @Pete-S said in Powershell countdown on servers?:

                                  @Grey

                                  If you wanted to show the progress % you could do something like this as well:

                                  $quantity = $systems.count
                                  $i = 0
                                  
                                  foreach ($server in $systems){
                                     //stuff
                                     $i = $i + 1
                                     $progress = 100 * $i / $quantity
                                  }
                                  

                                  Another trick is to not count it at all, just print a dot (".") for each iteration.
                                  .........

                                  Tested and this needs a try/catch for a divide by zero. The math is also off.
                                  9d767202-8760-4564-a5fa-0f1adc903aea-image.png

                                  You probably had --$quantity still in there. It decreases $quantity by one each time it executes.

                                  But use @Obsolesce example.

                                  1 Reply Last reply Reply Quote 0
                                  • 1 / 1
                                  • First post
                                    Last post