我如何要求确认?

时间:2019-10-01 16:14:02

标签: 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
    }
}

查看位$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)。我受困的是“您的选择是”,我不需要...如果需要,我只需要它就可以继续执行脚本。我该怎么办?

2 个答案:

答案 0 :(得分:1)

考虑使用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');

enter image description here

对于您的用例

# 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
}

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

答案 1 :(得分:0)

以下提示输入,将单击的值存储在变量中,然后将其作为字符串返回。检查并查看static void textBox_TextChanged(object sender, EventArgs e) { var textBox = sender as TextBox; textBox.Text = "0"; } 的值并从那里开始应该很简单。

$var