Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
911 views
in Technique[技术] by (71.8m points)

user interface - How to handle progress bar using PowerShell?

I want to capture an image with this method

DISM.exe /capture-ffu /imagefile=e:WinOEM.ffu /capturedrive=\.PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"

Reference:

https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/deploy-windows-using-full-flash-update--ffu

in the powershell I used this way

& DISM.exe /capture-ffu /imagefile=e:WinOEM.ffu /capturedrive=\.PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"

and It show the command window while capturing. Is it possible for me to create a GUI like progress bar while it doing capture?

Thanks for any advice.

I have created this PowerShell script, but I don't know where should I put this command

& DISM.exe /capture-ffu /imagefile=e:WinOEM.ffu /capturedrive=\.PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"



Add-Type -assembly System.Windows.Forms

## -- Create The Progress-Bar
$ObjForm = New-Object System.Windows.Forms.Form
$ObjForm.Text = "Image Capturing"
$ObjForm.WindowState = 'Maximized'
$ObjForm.BackColor = "White"

$ObjForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$ObjForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

## -- Create The Label
$ObjLabel = New-Object System.Windows.Forms.Label
$ObjLabel.Text = "Starting. Please wait ... "
$ObjLabel.Left = 5
$ObjLabel.Top = 10
$ObjLabel.Width = 500 - 20
$ObjLabel.Height = 15
$ObjLabel.Font = "Tahoma"
## -- Add the label to the Form
$ObjForm.Controls.Add($ObjLabel)

$PB = New-Object System.Windows.Forms.ProgressBar
$PB.Name = "PowerShellProgressBar"
$PB.Value = 0
$PB.Style="Continuous"

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 700 - 40
$System_Drawing_Size.Height = 20
$PB.Size = $System_Drawing_Size
$PB.Left = 5
$PB.Top = 40
$ObjForm.Controls.Add($PB)

## -- Show the Progress-Bar and Start The PowerShell Script
$ObjForm.Show() | Out-Null
$ObjForm.Focus() | Out-NUll
$ObjLabel.Text = "Starting. Please wait ... "
$ObjForm.Refresh()

Start-Sleep -Seconds 1

## -- Execute The PowerShell Code and Update the Status of the Progress-Bar
# $Result = Get-ChildItem -Path $Path -File -Recurse -Force | Select Name, @{Name="Path";Expression={$_.FullName}}
$Result = & DISM.exe /capture-ffu /imagefile=E:TESTcapture.ffu /capturedrive=\.PhysicalDrive0 /name:disk0
$Counter = 0
ForEach ($Item In $Result) {
    ## -- Calculate The Percentage Completed
    $Counter++
    [Int]$Percentage = ($Counter/$Result.Count)*100
    $PB.Value = $Percentage
    $ObjLabel.Text = "Capturing The Image"
    $ObjForm.Refresh()
    Start-Sleep -Milliseconds 150
    # -- $Item.Name
    "`t" + $Item.Path

}

$ObjForm.Close()
Write-Host "`n"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a lot of examples at StackOverflow on how to tackle this.
Basically, you need to make your script event driven, (see e.g.: Powershell Progress Bar in Windows Forms and Start-Sleep pauses entire function) and/or use separate treads (see e.g.: PowerShell: Job Event Action with Form not executed)

For more examples, see: https://stackoverflow.com/questions/linked/7414187?lq=1

In general: to start your script on the Loaded event, you might do something like this:

Function DISM {
    $Result = & DISM.exe /capture-ffu /imagefile=E:TESTcapture.ffu /capturedrive=\.PhysicalDrive0 /name:disk0
    $Counter = 0
    ForEach ($Item In $Result) {
        ...
    }
    $ObjForm.Close()
}

$ObjForm.Add_Loaded({DISM})
$ObjForm.Show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...