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!" }
-
@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.