New-Window and Get-PowerShellDataSource

Jul 18, 2011 at 9:30 PM


I've seen multiple examples where Get-PowerShellDataSource has been used with New-ListView by itself WITHOUT being wrapped in a window from New-Window; however, I've not seen any examples of Get-PowerShellDataSource being used with New-Window, and I'm wondering if this is even possible.  I am trying to create a UI to manipulate services on a given server.  I've got all of that working with no problem.  I have one function called Find-Services that gets the services...


Function Find-Services ($window) {
    $serverName = ($window | Get-ChildControl cboServerName).Text
    $txtNameFilter = ($window | Get-ChildControl txtNameFilter).Text
    $window.DataContext = (Get-Service -Name $txtNameFilter -ComputerName $serverName)


...and I have a ListView that will show the results...

New-ListView -Name lstServices -Row 7 -Column 0 -ColumnSpan 4 -Margin 5 -DataBinding @{ItemsSource = New-Binding} -View {
    New-GridView {
        New-GridViewColumn DisplayName
        New-GridViewColumn Status

...and all of that works great.  When I execute Find-Services though, it hangs on the UI thread (obviously, as it's having to connect to servers across the WAN to get statuses).  I wanted to use Get-PowerShellDataSource to make the getting of the service status asynchronous; however, no matter how I try to put it on the New-Window call, nothing ever happens.  Here is what I have tried so far.  Note that I am replacing the Find-Services function with just a direct call to Get-Service for the services on my machine to no avail.

New-Window -Title "Job Monitoring" -Width 400 -Height 400 -WindowStartupLocation CenterScreen -On_Loaded {
    Register-PowerShellCommand -ScriptBlock { $window.Content.DataContext.Script = $window.Content.DataContext.Script } -Run -In "0:0:2.0"
} -DataContext {
    Get-PowerShellDataSource -Script { Get-Service -Name '*' -ComputerName '.' | % { $_ } } 
} { ...view declaration here... } -Show

Has anyone encountered this problem and found a workaround, or am I approaching this in a way I should not be and trying to force the DataContext on the Window when I can't?