Capture cmdlet progress bars

I’m working on making a GUI for some winPE stuff to make it easier on our system admins. Right now I have the basic ps script working well. When I execute the dism cmdlets, a progress bar pops up in it’s own window. I’d like to bind that progress bar to my own progress bar in my forms. Is there a way to do that?

Can you provide a more specific example with the cmdlets that are producing the progress bars? You can probably show no progress and generate your own progress in the form. Another question is if you launch the cmdlets in a console, does the progress stay in the window or does it spawn a child process?

Well, you should be able to prevent the default progress bars from popping up by setting the $ProgressPreference variable to SilentlyContinue. However, getting the data into your own form would depend on how you are executing the PowerShell code. If you’re executing code using the System.Management.Automation.PowerShell class, then it’s easy; you have access to the Progress stream, and can bind listeners to its DataAdded event (PowerShell.Streams.Progress.DataAdded; see [url]http://msdn.microsoft.com/en-us/library/system.management.automation.psdatastreams.progress(v=vs.85).aspx[/url] and [url]http://msdn.microsoft.com/en-us/library/dd144531(v=vs.85).aspx[/url].

If you’re just executing a script in powershell.exe, and building your own forms from there, I’m not sure how you’d intercept those ProgressRecords, off the top of my head.

I’m doing the GUI in PowerShell Studio 2012. Here is an example some lines that pops up a progress bar:

Mount-WindowsImage -ImagePath $DestinationWimPath -Path $TempMountPath -Index 1

Add-WindowsPackage -Path $TempMountPath -PackagePath “C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPE_OCs\WinPE-WMI.cab”

I want to bind that progress bar to a progress bar in my GUI so I can show progress of each step. I know I could suppress all of the progress bars and show my own for each step, but I want to show the progress of the actual step, not the overall process. If powershell is outputting a progress bar, certainly there is a way I can get that info and bind to it.

Not sure what to tell you. If you search for “PowerShell Progress Stream”, you’ll find all sorts of examples that use the approach I mentioned, using the S.M.A. PowerShell object, then monitoring its Streams.Progress collection for new data. This is what the PowerShell host is doing to show you the progress bars in the first place, in fact.

I didn’t find any examples of a way to accomplish this when you’re just executing a cmdlet normally from within the PowerShell console.

If you’ve plenty of time on your hands, another way would be to execute these actions via the DISM API, which has a callback, DismProgressCallback . It’s a Win32 API if i recall though, so won’t be fun to setup.