Reputation: 1614
I am re-writing the Write-Progress function to work better with my script and to accomplish this I am joining all the arguments into a string and then trying to use it on the command however it is not working.
Function
Function Update-Progress {
Param (
[String] $Activity,
[Bool] $Completed,
[String] $CurrentOperation,
[Int] $ID,
[Int] $ParentID,
[Int] $PercentComplete,
[Int] $SecondsRemaining,
[Int] $SourceID,
[String] $Status
)
If ($htDisplay.WriteProgress.Enable -EQ $True -AND $htDisplay.WriteProgress.StopWatch.Elapsed.TotalMilliseconds -ge 500) {
$Parameters = New-Object System.Text.StringBuilder
IF (isNull($Activity)) { Write-Error "Activity is Required" } Else { $Null = $Parameters.Append("-Activity `"$Activity`"") }
IF (!(isNull($Completed))) { $Null = $Parameters.Append(" -Completed") }
IF (!(isNull($CurrentOperation))) { $Null = $Parameters.Append(" -CurrentOperation `"$CurrentOperation`"") }
IF (!(isNull($ID))) { $Null = $Parameters.Append(" -ID `"$ID`"") }
IF (!(isNull($ParentID))) { $Null = $Parameters.Append(" -ParentID `"$ParentID`"") }
IF (!(isNull($PercentComplete))) { $Null = $Parameters.Append(" -PercentComplete `"$PercentComplete`"") }
IF (!(isNull($SecondsRemaining))) { $Null = $Parameters.Append(" -SecondsRemaining `"$SecondsRemaining`"") }
IF (!(isNull($SourceID))) { $Null = $Parameters.Append("-SourceID `"$SourceID`"") }
IF (!(isNull($Status))) { $Null = $Parameters.Append(" -Status `"$Status`"") }
"Write-Progress $Parameters"
Write-Progress $Parameters
$htDisplay.WriteProgress.StopWatch.Reset()
$htDisplay.WriteProgress.StopWatch.Start()
}
}
Command Calling Function
Update-Progress -ID 1 -Activity "Preloading threads" -Status "Starting Job $($htConfig.MultiThread.Jobs.count)"
Error - The Progress bar displays
-Activity "Preloading Threads" -ID "1" -Status "Starting Job #"
Write-Host of the exact command shows the following so the syntax is correct, just need to figure out how to process the variable as all the parameters instead of just the activity param.
Write-Progress -Activity "Preloading threads" -ID "1" -Status "Starting Job 2"
Upvotes: 1
Views: 491
Reputation: 174485
to accomplish this I am joining all the arguments into a string and then trying to use it on the command however it is not working.
That's a terrible idea - use splatting with the $PSBoundParameters
automatic variable instead - it already contains everything you need:
Function Update-Progress {
Param (
[String] $Activity,
[Bool] $Completed,
[String] $CurrentOperation,
[Int] $ID,
[Int] $ParentID,
[Int] $PercentComplete,
[Int] $SecondsRemaining,
[Int] $SourceID,
[String] $Status
)
If ($htDisplay.WriteProgress.Enable -EQ $True -AND $htDisplay.WriteProgress.StopWatch.Elapsed.TotalMilliseconds -ge 500) {
Write-Progress @PSBoundParameters
$htDisplay.WriteProgress.StopWatch.Reset()
$htDisplay.WriteProgress.StopWatch.Start()
}
}
I would probably go for a Switch
rather than Bool
for boolean parameters. You can grab the exact Param
block from the original command with:
[System.Management.Automation.ProxyCommand]::GetParamBlock($(Get-Command Write-Progress))
And end up with getting input validation and positional parameters corresponding to what Write-Process
expects:
Function Update-Progress {
param(
[Parameter(Mandatory=$true, Position=0)]
[string]${Activity},
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[string]${Status},
[Parameter(Position=2)]
[ValidateRange(0, 2147483647)]
[int]${Id},
[ValidateRange(-1, 100)]
[int]${PercentComplete},
[int]${SecondsRemaining},
[string]${CurrentOperation},
[ValidateRange(-1, 2147483647)]
[int]${ParentId},
[switch]${Completed},
[int]${SourceId}
)
If ($htDisplay.WriteProgress.Enable -EQ $True -AND $htDisplay.WriteProgress.StopWatch.Elapsed.TotalMilliseconds -ge 500) {
Write-Progress @PSBoundParameters
$htDisplay.WriteProgress.StopWatch.Reset()
$htDisplay.WriteProgress.StopWatch.Start()
}
}
Upvotes: 3
Reputation: 1614
Ok, Thanks to someone else here I have found the answer. It was inline expansion and I needed to use a hash table variable.
Source: Inline expansion of powershell variable as cmdlet parameter?
Function Update-Progress {
Param (
[String] $Activity,
[Bool] $Completed,
[String] $CurrentOperation,
[Int] $ID,
[Int] $ParentID,
[Int] $PercentComplete,
[Int] $SecondsRemaining,
[Int] $SourceID,
[String] $Status
)
If ($htDisplay.WriteProgress.Enable -EQ $True -AND $htDisplay.WriteProgress.StopWatch.Elapsed.TotalMilliseconds -ge 500) {
IF (isNull($Activity)) { Write-Error "Activity is Required" } Else { $Parameters = @{ Activity=$Activity } }
IF (!(isNull($Completed))) { $Parameters += @{ Completed=$Completed } }
IF (!(isNull($CurrentOperation))) { $Parameters += @{ CurrentOperation=$CurrentOperation } }
IF (!(isNull($ID))) { $Parameters += @{ ID=$ID} }
IF (!(isNull($ParentID))) { $Parameters += @{ ParentID=$ParentID } }
IF (!(isNull($PercentComplete))) { $Parameters += @{ PercentComplete=$PercentComplete } }
IF (!(isNull($SecondsRemaining))) { $Parameters += @{ SecondsRemaining=$SecondsRemaining } }
IF (!(isNull($SourceID))) { $Parameters += @{ SourceID=$SourceID } }
IF (!(isNull($Status))) { $Parameters += @{ Status=$Status } }
Write-Progress @Parameters
$htDisplay.WriteProgress.StopWatch.Reset()
$htDisplay.WriteProgress.StopWatch.Start()
}
}
Upvotes: 0