Powershell 中的退出函数导致未处理的异常错误

Exit Function in Powershell causes Unhandled exception error

我在一个简单的 PowerShell 表单上重现了这个错误,它在我创建的每个代码中都不断发生。 我想使用退出按钮退出整个程序。但我得到例外。 有人可以帮忙吗?


        Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "press Exit to Quit"
    $Form.minimumSize = New-Object System.Drawing.Size(200, 200)
    $Form.maximumSize = New-Object System.Drawing.Size(200, 200)
    $Form.TopMost = "True"
    $Form.ShowInTaskbar = "True"
    $Form.StartPosition = "CenterScreen"
    $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
    
    $Exit = New-Object System.Windows.Forms.Button;
    $Exit.Text = "Exit"
    $Exit.Location = New-Object System.Drawing.Point(100, 50)
    $Exit.add_click({Exit}) 
    
    
    
    
    $Form.Controls.Add($Exit)
    
    
    $Form.ShowDialog()    



错误文本也是:

************** Exception Text **************
System.Management.Automation.ExitException: System error.
   at System.Management.Automation.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()
   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我不知道 $Exit.add_click({Exit}) 应该做什么。

$Exit.Add_Click({
    $Form.Close()
})