我如何要求确认?

Rj *_*Kay 4 powershell

我想创建一个确认框。我想要一个弹出框,显示这似乎是工厂密钥。你想继续吗?如果是,则继续执行脚本的其余部分;如果不是,则退出脚本。

以下是我的脚本:

if (Test-Path $USB2\sources\install3.swm) { 
    $caption = "*** IT APPEARS THIS IS A FACTORY KEY ***"    
    $message = "Are you Sure You Want To Proceed:"
    [int]$defaultChoice = 1
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "I understand, flash over it."
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
    $choiceRTN = $host.UI.PromptForChoice($caption, $message, $options, $defaultChoice)

    if ( $choiceRTN -ne 1 ) {
        "Your Choice was Yes"
    } else {
        Stop-Process -Id $PID
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅一点,$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)。我陷入困境的是“你的选择是”,我不需要它......我只需要它继续执行脚本(如果是)。我怎么做?

Ada*_*dam 5

考虑使用System.Windows.MessageBox...

Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('It appears that this is the factory key. Do you wish to continue?', 'Confirmation', 'YesNo');
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

对于您的用例,

# You need this to use System.Windows.MessageBox
Add-Type -AssemblyName 'PresentationFramework'

# By default, you're blasting the USB drive
$continue = 'Yes'

# If you see the file, prompt the user
if ( $(Test-Path -Path "${USB2}\sources\install3.swm") ) { 
    $caption = "*** IT APPEARS THIS IS A FACTORY KEY ***"    
    $message = "Are you Sure You Want To Proceed:"

    $continue = [System.Windows.MessageBox]::Show($message, $caption, 'YesNo');
}

if ($continue -eq 'Yes') {
    # The user either (a) said "yes" to the "do you prompt" or
    # (b) the file didn't exist.

    Write-Output "Flash over..."
    # ...do the flashing here...
} else {
    # The user said "no" to the flash prompt

    Write-Output "Terminating process..."
    Start-Sleep 1
}
Run Code Online (Sandbox Code Playgroud)

消息框将返回YesNo,您可以在控制流中使用它来确定后续步骤。如果这包括关闭窗口,请致电exit