Reputation: 684
I'm trying to use a PowerShell progress bar and the -status parameter isn't working as advertised (or I'm reading the documentation incorrectly.
For simplicity sake, let's say I have a collection:
@("red", "green", "blue")
... and I have another method which iterates through that collection. But when it iterates, the processing takes a long time. So I want to give status to the user by placing a call to progressBar($i, $s)
in the loop.
There's the code for progressBar:
function progressBar ($count, $color) {
Write-Progress -Activity "My Progress Bar" -status "Doing stuff on $color" -percentComplete ($count / $totalItems * 100)
}
Which is corresponds with:
where the author uses this example:
-status "Found Service $i"
But when I run my script, nothing shows up after "Doing stuff on" -- it's blank!
I always thought + was concatenation in PS... but doing this -status "Doing stuff on " + $color
fails too!
Before posting, I read this:
How do I concatenate strings and variables in PowerShell?
... and the suggestion to use $($color)
makes the cmdlet fail all together.
So, how can I get my entire string to appear in -status rather than it dropping the $color part?
Thanks!
===================================================
Hello again... I'm adding the full code per @BenH and @Persistent13's comments below.
$a = @("red", "green", "blue")
$counter = 1
$totalItems = $a.Count
foreach ($color in $a) {
Write-Host $color "`n"
Start-Sleep -s 3
$counter += 1
progressBar $counter, $color
}
function progressBar ($i, $s) {
Write-Progress -Activity "My Progress Bar" -status "Doing stuff on $s" -percentComplete ($i / $totalItems * 100)
}
Upvotes: 0
Views: 1252
Reputation: 10323
Here's a working example using the less modified version of your code. There were some issues that needed to be addressed.
First, let's see the "new" code.
function progressBar ($i, $s,$totalItems) {
Write-Progress -Activity "My Progress Bar" -status "Doing stuff on $s" -percentComplete ($i / $totalItems * 100)
}
$a = @("red", "green", "blue")
$counter = 0
$totalItems = $a.Count
foreach ($color in $a) {
Write-Host $color "`n"
Start-Sleep -s 3
$counter += 1
progressbar -i $counter -s $color -totalItems $totalItems
}
As you can see, the status and its color variable are properly displayed.
There was a number of mistake though in your original code.
$totalItems
inside your function but it is not made available to it through the function. That is not a good practice because it will fails if that variable is not defined in another script another time. -status "Doing stuff on " + $color
won't work while -status ("Doing stuff on " + $color)
will. Even simpler, the auto-expand feature when using double-quoted string -status "Doing stuff on $color"
Now, while the modified code I provided to you is working, I'd suggest a few other improvements.
Also, because you were using a $counter
variable, i'd personally had gone for a for loop instead of foreach so the counter is embedded without extra declaration.
Also, if you had a bigger array, it could have been worth it to calculate the offset right off to optimize the code.
function progressBar ($index,$Name, $Offset) {
$PercentComplete = 0
if ($index -gt 0) { $PercentComplete = $index * $Offset } # Avoid divide by 0 error for first item.
Write-Progress -Activity "My Progress Bar" -status "Doing stuff on $Name" -percentComplete ($PercentComplete)
}
$ColorArray = @("red", "green", "blue")
$Offset = 1 / $ColorArray.Count * 100 # Calculate one time only instead of each iterations of the loop.
# Because you use a counter, i'd use For instead of foreach.
for($i=0;$i -le $ColorArray.Count;$i++) {
$CurrentColor = $ColorArray[$i]
Write-Host $CurrentColor "`n"
Start-Sleep -s 3
progressbar -index $i -Name $CurrentColor -Offset $Offset
}
Finally, just thought I'd mention it...
Write-host
automatically appends a new line if you do not use the -nonewline
switch parameter. Maybe you wanted that extra blank line in there or maybe not... just thought it was worth to mention.
Upvotes: 2
Reputation: 1863
function progressBar ($Count, $color) {
Write-Progress -Activity "My Progress Bar" -CurrentOperation "Doing stuff on
$color" -percentComplete ($Count / $totalItems * 100)
}
$a = @("red", "green", "blue")
$counter = 0
$totalItems = $a.Count
foreach ($color in $a) {
Write-Host $color "`n"
progressBar -Count $counter -color $color
Start-Sleep -s 3
$counter += 1
}
So I cannot explain why -Status
is not showing data from variables. But looking back through all my progress bars I have never used it. Using -CurrentOperation
Displays the variable contents on the bar. You had a number of syntax mistakes.
Upvotes: 1