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)
}