I’ve been playing around with PoshRSJob and have run into an obstacle that I was hoping someone
might have some insight into.
I’ve created a function in powershell which manages my downloads from a git (bitbucket) repository, since there can oftentimes be pulls of hundreds of files, I’m using PoshRSJob to improve my download experience. Everything works great, except I can’t seem to figure out how to pass the results from the various runspaces to update my WPF xaml form Progress bar.
Is there something I’m missing when it comes to PoshRSJob that enables this kind of functionality? Here’s an example I tried to put together to see if you wouldn’t mind looking at it?
#if(!(Get-Module -ListAvailable | Where-Object Name -eq "PoshRSJob")){Install-Module -Name PoshRSJob -MaximumVersion 1.7.2.7 -Force} $AppFiles = 1..100 | ForEach-Object{ New-Object PSObject -Property @{ FileName="File_$_"; Size=$(Get-Random -minimum 2000 -maximum 30000) } } #region Load External Assemblies Add-Type -AssemblyName PresentationFramework [System.Reflection.Assembly]::LoadFrom((Get-ChildItem -Path "C:\Program Files (x86)\Microsoft SDKs" -Recurse | Where-Object Name -eq "Microsoft.Expression.Drawing.dll")[0].FullName) | Out-Null #endregion #region Load Xaml into Powershell using XamlReader, and put Named nodes from the form into Powershell objects [xml]$Xaml = @"<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:fa="http://schemas.fontawesome.io/icons/" xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" Title="Installer - Shared Datamart" Height="500" Width="700" ResizeMode="CanMinimize"><!--#region Download Progress Bar--><Grid Grid.Column="1" Grid.Row="0"><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid Grid.Row="0" Width="150" Height="150"><Ellipse Fill="#d6d6d6" Margin="0"/><ed:Arc Name="tabItemDownloadProgressIndicator" ArcThickness="10000" ArcThicknessUnit="Pixel" EndAngle="0" Fill="#00aeef" Margin="0" Stretch="None" StartAngle="0"/><Path Data="M122.12466,50.500001 C161.68188,50.500001 193.74933,79.720514 193.74933,115.76583 193.74933,151.81116 161.68188,181.03166 122.12466,181.03166 82.567459,181.03166 50.5,151.81116 50.5,115.76583 50.5,79.720514 82.567459,50.500001 122.12466,50.500001 z M122.12466,15.500002 C115.11131,15.500001 108.06759,16.15595 101.17562,17.455171 94.320358,18.747478 87.555672,20.68768 81.06208,23.236789 74.633308,25.76045 68.413475,28.90369 62.578602,32.598592 56.821587,36.244188 51.387154,40.460622 46.441212,45.147973 41.568821,49.765626 37.124958,54.88453 33.256836,60.370923 29.443533,65.779557 26.152985,71.598901 23.502686,77.662767 20.881958,83.658967 18.861307,89.955377 17.517124,96.359735 16.181229,102.7246 15.5,109.26228
15.499998,115.76583 15.5,121.88733 16.102737,128.03735 17.287348,134.04314 18.478497,140.08206 20.268742,146.0313 22.59779,151.72893 24.950483,157.48442 27.873993,163.03594 31.274794,168.24126 34.720432,173.51521 38.686134,178.48097 43.053432,183.0212 47.483006,187.62616 52.363304,191.83334 57.559284,195.55202 62.824764,199.32044 68.457726,202.61882 74.313492,205.3822 80.232521,208.17543 86.426445,210.44476 92.745743,212.14876 99.109169,213.86464
105.64935,215.02091 112.21479,215.59747 118.79604,216.17543 125.45329,216.17543 132.03455,215.59747 138.59999,215.02091 145.14017,213.86464 151.50359,212.14876 157.82289,210.44476 164.01682,208.17543 169.93585,205.3822 175.79161,202.61882 181.42458,199.32044 186.69005,195.55202 191.88603,191.83334 196.76633,187.62616 201.19589,183.0212 205.5632,178.48097 209.5289,173.51521 212.97453,168.24126 216.37534,163.03594 219.29884,157.48442 221.65155,151.72893
223.98059,146.0313 225.77084,140.08206 226.96199,134.04314 228.14659,128.03735 228.74933,121.88733 228.74933,115.76583 228.74933,109.26228 228.0681,102.7246 226.73221,96.359735 225.38803,89.955377 223.36737,83.658967 220.74664,77.662767 218.09634,71.598901 214.8058,65.779557 210.99249,60.370923 207.12437,54.88453 202.68051,49.765626 197.80812,45.147973 192.86218,40.460622 187.42775,36.244188 181.67073,32.598592 175.83586,28.90369 169.61603,25.76045
163.18726,23.236789 156.69366,20.68768 149.92897,18.747478 143.07372,17.455171 136.18175,16.15595 129.13803,15.500001 122.12466,15.500002 z M122.12467,0.5 C189.29613,0.50000119 243.74933,52.106267 243.74933,115.76583 243.74933,179.4254 189.29611,231.03166 122.12466,231.03166 54.953205,231.03166 0.5,179.4254 0.5,115.76583 0.5,52.106267 54.95322,0.50000119 122.12467,0.5 z" Fill="White" Margin="-10" Stretch="Fill"/><TextBlock Name="tabItemDownloadProgressValue" Text="0%" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8 0 0 0" FontSize="25"/></Grid><StackPanel Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center"><TextBlock FontStyle="Italic" FontSize="11"><Run Text="Total Size:"/><Run Name="tabItemDownloadProgressTotalSize" Text="0 / 0 kb"/></TextBlock><TextBlock FontStyle="Italic" FontSize="11"><Run Text="Total Files:"/><Run Name="tabItemDownloadProgressTotalFiles" Text="0 / 0"/></TextBlock></StackPanel><Button Grid.Row="2" Name="tabItemDownloadPromptButton" Content="Download" FontSize="20" Height="45" FontWeight="Bold" Cursor="Hand" Margin="0 0 0 10" Visibility="Visible"/></Grid><!--#endregion--></Window>"@ $Reader=(New-Object System.Xml.XmlNodeReader $Xaml) try{$Form=[Windows.Markup.XamlReader]::Load( $Reader )} catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit} $Xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)} #endregion $TotalSize = 0 ForEach($App in $AppFiles){ $TotalSize += $App.Size } $tabItemDownloadPromptButton.Add_Click({ $ScriptBlock = { $tabItemDownloadProgressIndicator.EndAngle += $($_.Size/$Using:TotalSize * 360) } $AppFiles | Start-RSJob -Throttle 20 -Name {$_.FileName} -Batch ([guid]::NewGuid()) -ScriptBlock $ScriptBlock | Wait-RSJob -ShowProgress }) <# $tabItemDownloadProgressIndicator.EndAngle = $_.Size/$TotalSize * 360; #> $Form.ShowDialog()