如何使用 PowerShell 处理 GUI return errorlevel?

How do I return errorlevel by handling GUI using PowerShell?

我有一个 GUI,表单会在 10 秒后自动关闭。但是如果我们点击暂停按钮,它也会关闭表单。我想 return 我所做的每个过程的错误级别。如果表单自动关闭,它将 return 错误级别 10。但是如果我们单击按钮暂停,它将 return 错误级别 20。请任何人提供帮助。

这是我的代码。

    function Timer_GUI {
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $label1 = New-Object 'System.Windows.Forms.Label'
    $label2 = New-Object 'System.Windows.Forms.Label'
    $Cancel = New-Object 'System.Windows.Forms.Button'
    $timer1 = New-Object 'System.Windows.Forms.Timer'

    $form1_Load = { 
        $TotalTime = 10 #in seconds
            $script:StartTime = (Get-Date).AddSeconds($TotalTime)
            #Start the timer
            $timer1.Start()
    }
    $label1.Location = New-Object System.Drawing.Size(220,60)
    $label1.Size = New-Object System.Drawing.Size(500,30)

    $label2.Text = "The Process Will Continue in 10s"
    $label2.Location = New-Object System.Drawing.Size(140,30)
    $label2.Size = New-Object System.Drawing.Size(500,30)

    $form1.SuspendLayout()
    $form1.Controls.Add($label1)
    $form1.Controls.Add($label2)
    $form1.Controls.Add($Cancel)
    $form1.Width = 500
    $form1.Height = 200
    $form1.StartPosition = "CenterScreen"
    $form1.BackColor = "#e2e2e2"
    $form1.add_Load($form1_Load)

    $Cancel.DialogResult = 'Cancel'
    $Cancel.Location = New-Object System.Drawing.Size(350,100)
    $Cancel.Size = New-Object System.Drawing.Size(100,30)
    $Cancel.Text = "Pause"
    $Cancel.add_Click($Cancel_Click)
    $timer1.add_Tick($timer1_Tick)
    $form1.ResumeLayout()


    #Show the Form
    return $form1.ShowDialog()
    exit 100
}
#Call the form
Timer_GUI | Out-Null

不是 GUI 专家,但是,就在这里。添加全局变量 $Global:formresult 默认设置为 10,如果单击按钮设置为 20。

添加或更新了以下 3 行,

Add-Type -AssemblyName System.Windows.Forms
$Global:formresult = 10
$Cancel.add_Click({ $Global:formresult = 20 })
$Global:formresult

完整代码,从您的代码中复制并粘贴。

Add-Type -AssemblyName System.Windows.Forms
function Timer_GUI {
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $label1 = New-Object 'System.Windows.Forms.Label'
    $label2 = New-Object 'System.Windows.Forms.Label'
    $Cancel = New-Object 'System.Windows.Forms.Button'
    $timer1 = New-Object 'System.Windows.Forms.Timer'
    $Global:formresult = 10

    $form1_Load = { 
        $TotalTime = 10 #in seconds
            $script:StartTime = (Get-Date).AddSeconds($TotalTime)
            #Start the timer
            $timer1.Start()
    }
    $label1.Location = New-Object System.Drawing.Size(220,60)
    $label1.Size = New-Object System.Drawing.Size(500,30)

    $label2.Text = "The Process Will Continue in 10s"
    $label2.Location = New-Object System.Drawing.Size(140,30)
    $label2.Size = New-Object System.Drawing.Size(500,30)

    $form1.SuspendLayout()
    $form1.Controls.Add($label1)
    $form1.Controls.Add($label2)
    $form1.Controls.Add($Cancel)
    $form1.Width = 500
    $form1.Height = 200
    $form1.StartPosition = "CenterScreen"
    $form1.BackColor = "#e2e2e2"
    $form1.add_Load($form1_Load)

    $Cancel.DialogResult = 'Cancel'
    $Cancel.Location = New-Object System.Drawing.Size(350,100)
    $Cancel.Size = New-Object System.Drawing.Size(100,30)
    $Cancel.Text = "Pause"
    $Cancel.add_Click({ $Global:formresult = 20 })
    $timer1.add_Tick($timer1_Tick)
    $form1.ResumeLayout()


    #Show the Form
    return $form1.ShowDialog()
    exit 100
}
#Call the form
Timer_GUI | Out-Null

$Global:formresult