gpt4 book ai didi

powershell - 尝试..捕捉..最终在读取主机期间失败

转载 作者:行者123 更新时间:2023-12-02 23:32:53 27 4
gpt4 key购买 nike

当脚本执行 Read-Host cmdlet,关闭窗口不会激活 finally堵塞。下面是一个随意但功能最少的示例。我正在使用 PowerShell 5.0。 Beep() 只是为了让 finally 块执行变得明显。

try {
$value= Read-Host -Prompt "Input"
sleep 5
} finally {
[Console]::Beep(880,1000)
}
  • 如果您在 Read-Host 期间单击红色 X 关闭窗口finally块不会执行。
  • 如果您在 sleep 期间单击红色 X 关闭窗口这finally块将执行。
  • 如果你在任何时候用 Ctrl-C 打断,finally块将执行。

  • 关于为什么 finally,我是否缺少一些基本的东西?在 Read-Host 期间关闭窗口时块不执行?
    完整案例涉及在 Amazon Snowball 设备上启动服务,如果脚本关闭,则需要停止服务。完整的案例行为反射(reflect)了上面的示例案例。
    编辑:由于评论说 $input 是保留变量,因此将变量从 $input 更改为 $value。不改变行为。

    最佳答案

    继续我的评论。
    控制台主机有点不灵活,这取决于您从它 native 执行的操作。 “X”与 PowerShell session /进程有关,而不是其中运行的代码。因此,为什么 CRTL+C 起作用,因为您正在停止代码运行,而不是 PowerShell session /进程。
    这里有几种方法可以让您考虑您的选择。

    ###############################################################################
    #region Begin initialize environment #
    ###############################################################################

    # Initialize GUI resources
    Add-Type -AssemblyName System.Drawing,
    PresentationCore,
    PresentationFramework,
    System.Windows.Forms,
    microsoft.VisualBasic
    [System.Windows.Forms.Application]::EnableVisualStyles()

    ###############################################################################
    #endregion End initialize environment #
    ###############################################################################

    # Prevent the MessageBox UI from closing until an entry is made
    while (
    ($UserEntry = [Microsoft.VisualBasic.Interaction]::
    InputBox('Enter a Host/User', 'Add Item')) -eq ''
    )
    {
    [System.Windows.Forms.MessageBox]::
    Show(
    'Entry cannot be empty',
    "Error on close" ,
    0,
    [System.Windows.MessageBoxImage]::Error
    )
    }
    "You entered $UserEntry"
    或完整的自定义表单以进行更精细的控制
    # Initialize the form object
    $form = New-Object System.Windows.Forms.Form

    # Define form elements
    $form.Text = 'Data Entry'

    $txtUserInput = New-Object system.Windows.Forms.TextBox
    $txtUserInput.multiline = $false
    $txtUserInput.width = 120
    $txtUserInput.height = 20
    $txtUserInput.location = New-Object System.Drawing.Point(40,29)
    $txtUserInput.Font = 'Microsoft Sans Serif,10'

    $form.controls.AddRange(@(
    $txtUserInput
    )
    )

    # Evaluate form events
    $form.Add_Closing(
    {
    param
    (
    $Sender,$Event
    )

    $result = [System.Windows.Forms.MessageBox]::Show(
    'Are you sure you want to exit?',
    'Close',
    [System.Windows.Forms.MessageBoxButtons]::YesNo
    )

    if ($result -ne [System.Windows.Forms.DialogResult]::Yes)
    {$Event.Cancel = $true}
    })

    # Start the form
    $form.ShowDialog() | Out-Null

    # Resource disposal
    $form.Dispose()

    关于powershell - 尝试..捕捉..最终在读取主机期间失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63542078/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com