views:

603

answers:

1

I am currently working on a project that requires me to put a progressbar in one of the panels of a statusbar. Does anyone have any experience with this, or can anyone provide any input or direction on how it is done. I have been searching for 2 days now for a solution with no luck. There is still not an abundance of powershell help out there, especially when it comes to windows forms.

+4  A: 

This is relatively simple to do with PowerShell data sources in WPK.

You can get WPK as part of the PowerShellPack.

Here's a pretty decent progress viewer written in WPK:

New-Grid -Columns 2 {
    New-TextBlock -Margin 10 -TextWrapping Wrap -ZIndex 1 -HorizontalAlignment Left -FontWeight Bold -FontSize 12 -DataBinding @{
        "Text" = "LastProgress.Activity"
    }
    New-TextBlock -Margin 10 -ZIndex 1 -TextWrapping Wrap -Column 1 -VerticalAlignment Bottom -HorizontalAlignment Right -FontStyle Italic -FontSize 12 -DataBinding @{
        "Text" = "LastProgress.StatusDescription"
    }
    New-ProgressBar -ColumnSpan 2 -MinHeight 250 -Name ProgressPercent -DataBinding @{
        "Value" = "LastProgress.PercentComplete"
    }
} -DataContext {
    Get-PowerShellDataSource -Script { 
        foreach ($n in 1..100) {
            Write-Progress "MajorProgress" "MinorProgress" -PercentComplete $n
            Start-Sleep -Milliseconds 250
        }
    }
} -AsJob

The PowerShellDataSource returns an object with a list of all of the outputs for a given stream and the last item outputted on the given stream (i.e. Progress and LastProgress). In order to display the progress bar, we need bind to the LastProgress property.

The first half of the code declares the progress bar. By using the -DataBinding parameter the TextBlocks and Progress bars will automatically sync to the data context. Data context can either be declared at this level (as is shown in the example) or can be in a parent control.

The DataContext in this example is a simple PowerShell script that uses Write-Progress to output a test message every quarter second, but you can use any script you'd like.

Hope this helps.

Start-Automating
I am currently trying to run the powershellpack ise and getting a runtime error r6034. Trying to test the above fix, but unable to get past this issue.
stratrider