如何为现有PowerShell WinForms GUI添加文本框并输出脚本运行结果
实现方法
你当前弹出独立窗口的核心原因是使用了Start-Process调用脚本,会启动独立的PowerShell进程运行,无法捕获输出。按以下方案修改即可实现输出内嵌展示:
核心修改点
- 新增只读多行文本框用于展示脚本输出,匹配原有黑色界面风格
- 替换
Start-Process调用逻辑,改用后台作业运行脚本,既不阻塞主界面,也能实时捕获所有输出(含报错信息) - 补全原有代码遗漏的OKButton控件定义,修改后可直接运行
修改后完整代码
$x = @()# the return variable Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $objForm = New-Object System.Windows.Forms.Form $objForm.Size = New-Object System.Drawing.Size(850,680) $objForm.BackColor = "Black" $objForm.StartPosition = "CenterScreen" $objForm.FormBorderStyle = 'Fixed3D' $objForm.MaximizeBox = $false $objForm.KeyPreview = $True $objForm.Topmost = $false # 补全原有代码遗漏的OKButton定义 $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(10,170) $OKButton.Size = New-Object System.Drawing.Size(100,30) $OKButton.Text = "运行" $OKButton.ForeColor = "White" $OKButton.BackColor = "Gray" $objForm.Controls.Add($OKButton) # 新增输出文本框 $outputTextBox = New-Object System.Windows.Forms.TextBox $outputTextBox.Multiline = $true $outputTextBox.ReadOnly = $true $outputTextBox.ScrollBars = "Vertical" $outputTextBox.Location = New-Object System.Drawing.Size(350,10) $outputTextBox.Size = New-Object System.Drawing.Size(470,620) $outputTextBox.BackColor = "Black" $outputTextBox.ForeColor = "LightGreen" $outputTextBox.Font = "Consolas,10" $objForm.Controls.Add($outputTextBox) $OKButton.Add_Click({ $selected = $objCombobox.SelectedItem $script:x += $objCombobox.SelectedItem if (![string]::IsNullOrWhiteSpace($selected)) { $outputTextBox.Clear() $outputTextBox.AppendText("=== 开始运行 $selected ===" + [Environment]::NewLine) switch ($selected) { "program 1" { # 替换Start-Process,用后台作业运行脚本避免UI阻塞,捕获所有输出 $job = Start-Job -ScriptBlock { param($scriptPath) # 绕过执行策略限制运行网络路径脚本 & powershell.exe -ExecutionPolicy Bypass -File $scriptPath 2>&1 } -ArgumentList "\\server1\folder1\script1.ps1" # 定时检查作业状态,获取输出 $timer = New-Object System.Windows.Forms.Timer $timer.Interval = 500 $timer.Add_Tick({ if ($job.State -ne 'Running') { # 作业结束,获取剩余输出 $output = Receive-Job -Job $job if ($output) { $outputTextBox.AppendText($output + [Environment]::NewLine) $outputTextBox.ScrollToCaret() } $outputTextBox.AppendText("=== 运行结束 ===" + [Environment]::NewLine) $timer.Stop() $timer.Dispose() Remove-Job -Job $job return } # 实时获取输出 $output = Receive-Job -Job $job if ($output) { $outputTextBox.AppendText($output + [Environment]::NewLine) $outputTextBox.ScrollToCaret() } }) $timer.Start() } } $objLabel.Text = "INSTALLATIONS" $objCombobox.SelectedIndex = -1 } }) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,120) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "INSTALLATIONS" $objLabel.Forecolor = "White" $objLabel.Backcolor = "Transparent" $objLabel.Font = "Arial,8,style=bold" $objForm.Controls.Add($objLabel) $objCombobox = New-Object System.Windows.Forms.Combobox $objCombobox.Text = "please choose" $objCombobox.Location = New-Object System.Drawing.Size(10,140) $objCombobox.Size = New-Object System.Drawing.Size(320,20) $objCombobox.Height = 500 [void] $objCombobox.Items.Add("program 1") $objForm.Controls.Add($objCombobox) $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog()
可选调整项
- 可自行修改
$outputTextBox的Location和Size参数调整文本框的位置和大小 - 可修改
ForeColor参数调整输出文字的颜色,比如改成白色、黄色等 - 如果脚本不需要实时输出,也可以直接用
$output = & \\server1\folder1\script1.ps1 2>&1直接运行后一次性输出,代码更简单但运行时界面会暂时卡住
内容的提问来源于stack exchange,提问作者chkdsk
相关产品推荐
相关产品推荐

