如何使用 PowerShell 处理进度条? [英] How to handle progress bar using PowerShell?

查看:119
本文介绍了如何使用 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"

参考:

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

在powershell中我是这样使用的

in the powershell I used this way

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

并且它在捕获时显示命令窗口.我可以在捕获时创建一个类似于进度条的 GUI 吗?

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

感谢您的建议.

我已经创建了这个 PowerShell 脚本,但我不知道我应该把这个命令放在哪里

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:\TEST\capture.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"

推荐答案

StackOverflow 上有很多关于如何解决这个问题的例子.
基本上,您需要使您的脚本事件驱动,(参见例如:Windows 窗体中的 Powershell 进度条Start-Sleep暂停整个函数)和/或使用单独的线程(参见例如:PowerShell:未执行表单的作业事件操作)

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)

更多示例,请参见:https://stackoverflow.com/questions/linked/7414187?lq=1

一般来说:要在 Loaded 事件上启动您的脚本,您可以执行以下操作:

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:\TEST\capture.ffu /capturedrive=\\.\PhysicalDrive0 /name:disk0
    $Counter = 0
    ForEach ($Item In $Result) {
        ...
    }
    $ObjForm.Close()
}

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

这篇关于如何使用 PowerShell 处理进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆