如何通过在Powershell中单击GUI形式的按钮来返回特定的错误级别?

问题描述 投票:0回答:1

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

Add-Type -AssemblyName System.Windows.Forms $ Font = New-Object 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()
powershell user-interface errorlevel
1个回答
1
投票

使用全局变量存储结果。

$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
© www.soinside.com 2019 - 2024. All rights reserved.