如何在Powershell中通过点击GUI形式的按钮来return指定错误级别?

How to return specifi error level by clicking a button in a form of GUI in Powershell?

我将使用 GUI 并单击按钮 return 具有特定数字的错误级别。它 return 是 $Auto_Button 的错误级别值,当脚本执行时 GUI 消失。

添加类型 -AssemblyName System.Windows.Forms $Font = 新对象 System.Drawing.Font("Times New Roman",13)

$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "Process"
$MainForm.Width = 500
$MainForm.Height = 200
$MainForm.StartPosition = "CenterScreen"
$MainForm.BackColor = "#e2e2e2"

$Title = New-Object System.Windows.Forms.Label
$Title.Font = $Font
$Title.Text = "Which process do you want to choose?"
$Title.Location = New-Object System.Drawing.Size(100,30)
$Title.Size = New-Object System.Drawing.Size(500,30)
$MainForm.Controls.Add($Title)

$Auto_Button = {$MainForm.Close()}
               Exit 10   

$Manual_Button = $MainForm.Close()}
               Exit 30

$Automatic = New-Object System.Windows.Forms.Button
$Automatic.Location = New-Object System.Drawing.Size(110,80)
$Automatic.Size = New-Object System.Drawing.Size(100,30)
$Automatic.Text = "Automatically"
$Automatic.Font = 'Microsoft Sans Serif,10'
$Automatic.BackColor = "#e47104"
$Automatic.Add_Click($Auto_Button)
$MainForm.Controls.Add($Automatic)

$Manual = New-Object System.Windows.Forms.Button
$Manual.Location = New-Object System.Drawing.Size(270,80)
$Manual.Size = New-Object System.Drawing.Size(100,30)
$Manual.Text = "Manually"
$Manual.Font = 'Microsoft Sans Serif,10'
$Manual.BackColor = "#e47104"
$Manual.Add_Click($Manual_Button)
$MainForm.Controls.Add($Manual)

$MainForm.ShowDialog()

使用全局变量存储结果。

$Auto_Button = ({ $global:result=10
                  $MainForm.Close() })

$Manual_Button = ({ $global:result=20
                    $MainForm.Close() })

...

$result=0
$MainForm.ShowDialog()

$result | Out-File .\exit.txt
exit $result