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.
Because you never change
$quantity
-
$quantity = $systems.count foreach ($server in $systems){ //stuff $quantity = $quantity-1 }
-
@JaredBusch 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.
Because you never change
$quantity
$newquant is where I want to do the math, inside the foreach.
-
@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.
-
@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 }
-
@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.
-
Are you doing this so you can have a progress bar or what?
-
@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.
-
@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!" }
-
@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.
-
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]" }
-
@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
-
@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.
-
@Grey said in Powershell countdown on servers?:
$newquant = $quantity-1
This always returns a fixed value, though.
Do it like this:
$newquant = --$quantity
-
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.
......... -
@Pete-S said in Powershell countdown on servers?:
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.
-
@Grey said in Powershell countdown on servers?:
@Pete-S said in Powershell countdown on servers?:
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.
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. -
@Grey said in Powershell countdown on servers?:
@Pete-S said in Powershell countdown on servers?:
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.
You probably had
--$quantity
still in there. It decreases $quantity by one each time it executes.But use @Obsolesce example.