如何使用Powershell捕获DISM输出并通过进度条反映状态?

如何使用Powershell捕获DISM输出并通过进度条反映状态?,powershell,Powershell,我想用这种方法拍摄一幅图像 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-

我想用这种方法拍摄一幅图像

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里我用这种方式

& DISM.exe /capture-ffu /imagefile=e:\WinOEM.ffu /capturedrive=\\.\PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"
并在捕获时显示命令窗口。 我是否可以在执行捕获时创建一个类似GUI的进度条

谢谢你的建议

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

$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"
我已经创建了这个PowerShell脚本。但在执行这项任务时

$Result = & DISM.exe /capture-ffu /imagefile=E:\TEST\capture.ffu /capturedrive=\\.\PhysicalDrive0 /name:disk0
进度条不是开始或显示进度,它只是显示为空(白色条)。 请给我任何建议

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

$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"
更新 我认为DISM.exe无法报告是否完成。 我必须让进度条一直滚动直到这个过程结束

& DISM.exe /capture-ffu /imagefile=e:\WinOEM.ffu /capturedrive=\\.\PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"
任何人都可以告诉我你的想法。

就足够了吗

.....
ForEach ($Item In $Result) {
    $Counter++
    [Int]$Percentage = ($Counter/$Result.Count)*100
    Write-Progress -activity "Completion" -status "Status" -PercentComplete $Percentage   
}

嗨@Tatu,我试过了,但它和我的解决方案是一样的,进度条没有分层,只是在捕获时显示白色条。我的意思是,在执行此
$Result=&DISM.exe/capture ffu/imagefile=E:\TEST\capture.ffu/capturedrive=\\\物理驱动器0/name:disk0
时,您的问题更多的是从DISM捕获输出。您需要在后台启动它并捕获输出。例如,见。