Redirect Process Output - Dism - ProgressBar

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
localpct
Posts: 309
Joined: Wed Sep 09, 2015 12:53 pm

Redirect Process Output - Dism - ProgressBar

Post by localpct » Thu Feb 13, 2020 9:52 am

Hello,
With DISM, it provides us a nice percentage of the process output:

Code: Select all

[===========================76.2%============              ] 
[===========================77.1%============              ] 
[===========================78.1%=============             ] 
[===========================78.9%=============             ] 
[===========================79.9%==============            ] 
[===========================80.8%==============            ]
How can I extract the percentage and apply it to a progress bar on the update step?

jvierra
Posts: 14145
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Redirect Process Output - Dism - ProgressBar

Post by jvierra » Thu Feb 13, 2020 9:56 am

What have you tried?

User avatar
localpct
Posts: 309
Joined: Wed Sep 09, 2015 12:53 pm

Re: Redirect Process Output - Dism - ProgressBar

Post by localpct » Thu Feb 13, 2020 11:20 am

RemoteDism.psf
(38.6 KiB) Downloaded 4 times
I had to regex most of the items ( line 91 )
Take what was left so if it's at 5.0%, it returns 50, if it's at 99.9 percent, it returns 999

Line 115 in the updatescript
$progressbar1.Value = $richtextbox1.Text

jvierra
Posts: 14145
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Redirect Process Output - Dism - ProgressBar

Post by jvierra » Thu Feb 13, 2020 11:29 am

You need to use a proper RegEx. The RegEx you are using is removing the decimal point.

Example:

Code: Select all

if('[===========================78.1%=============             ]' -match '(\d+.\d+)'){$matches[1]}

User avatar
localpct
Posts: 309
Joined: Wed Sep 09, 2015 12:53 pm

Re: Redirect Process Output - Dism - ProgressBar

Post by localpct » Thu Feb 13, 2020 11:35 am

I didn't think the Pbar can handle the decimals? that's why I did it

jvierra
Posts: 14145
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Redirect Process Output - Dism - ProgressBar

Post by jvierra » Thu Feb 13, 2020 11:48 am

Then you can just do this:

Code: Select all

$s = '[===========================78.1%=============             ]'
if($s -match '(\d+)'){[int]$matches[1]}

User avatar
localpct
Posts: 309
Joined: Wed Sep 09, 2015 12:53 pm

Re: Redirect Process Output - Dism - ProgressBar

Post by localpct » Fri Feb 14, 2020 7:24 am

So I just did this and it appears to be working

Code: Select all

RedirectOutputScript			  = {
			# Use $_.Data to access the output text
			$richtextboxOutput.AppendText($_.Data)
			$richtextboxOutput.AppendText("`r`n")
			if ($_.Data -match '(\d+)')
			{
				$progressbar1.Value = [int]$matches[1]
			}

Post Reply