如何在PowerShell中使用-confirm

时间:2014-07-09 08:42:57

标签: powershell

我正在尝试接受用户输入,在继续操作之前,我希望在屏幕上显示一条消息,而不是确认,无论用户是否要继续操作。我正在使用以下代码,但它不起作用:

write-host "Are you Sure You Want To Proceed:"  -Confirm

10 个答案:

答案 0 :(得分:78)

-Confirm是大多数PowerShell cmdlet中的一个开关,它强制cmdlet请求用户确认。您实际需要的是Read-Host cmdlet:

$confirmation = Read-Host "Are you Sure You Want To Proceed:"
if ($confirmation -eq 'y') {
  # proceed
}

或主机用户界面的PromptForChoice()方法:

$message  = 'something'
$question = 'Are you sure you want to proceed?'

$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))

$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) {
  Write-Host 'confirmed'
} else {
  Write-Host 'cancelled'
}

答案 1 :(得分:16)

这是一个简单的循环,除非用户选择' y'或者' n'

$confirmation = Read-Host "Ready? [y/n]"
while($confirmation -ne "y")
{
    if ($confirmation -eq 'n') {exit}
    $confirmation = Read-Host "Ready? [y/n]"
}

答案 2 :(得分:11)

Read-Host-Confirm没有影响的cmdlet的一个示例。-Confirm是PowerShell的常用参数之一,特别是风险缓解参数,当cmdlet即将对Windows PowerShell环境之外的系统进行更改时使用。许多但不是所有cmdlet都支持-Confirm风险缓解参数。

作为替代方案,以下是使用Read-Host cmdlet和正则表达式测试来获取用户确认的示例:

$reply = Read-Host -Prompt "Continue?[y/n]"
if ( $reply -match "[yY]" ) { 
    # Highway to the danger zone 
}

Remove-Variable cmdlet是一个说明-confirm开关用法的示例。

Remove-Variable 'reply' -Confirm

其他参考文献:CommonParametersWrite-HostRead-HostComparison OperatorsRegular ExpressionsRemove-Variable

答案 3 :(得分:6)

write-host没有-confirm参数。

你可以这样做:

    $caption = "Please Confirm"    
    $message = "Are you Sure You Want To Proceed:"
    [int]$defaultChoice = 0
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Do the job."
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Do not do the job."
    $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
{
   "Your Choice was NO"
}

答案 4 :(得分:2)

这是我使用的解决方案,类似于Ansgar Wiechers'溶液;

$title = "Lorem"
$message = "Ipsum"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "This means Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "This means No"

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice($title, $message, $Options, 0)

Switch ($result)
     {
          0 { "You just said Yes" }
          1 { "You just said No" }
     }

答案 5 :(得分:2)

基于Ansgar Wiechers's answer的稍微漂亮的功能。它是否真的更有用是一个有争议的问题。

function Read-Choice(
   [Parameter(Mandatory)][string]$Message,
   [Parameter(Mandatory)][string[]]$Choices,
   [Parameter(Mandatory)][string]$DefaultChoice,
   [Parameter()][string]$Question='Are you sure you want to proceed?'
) {
    $defaultIndex = $Choices.IndexOf($DefaultChoice)
    if ($defaultIndex -lt 0) {
        throw "$DefaultChoice not found in choices"
    }

    $choiceObj = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]

    foreach($c in $Choices) {
        $choiceObj.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList $c))
    }

    $decision = $Host.UI.PromptForChoice($Message, $Question, $choiceObj, $defaultIndex)
    return $Choices[$decision]
}

使用示例:

PS> $r = Read-Choice 'DANGER!!!!!!' '&apple','&blah','&car' '&blah'

DANGER!!!!!!
Are you sure you want to proceed?
[A] apple  [B] blah  [C] car  [?] Help (default is "B"): c
PS> switch($r) { '&car' { Write-host 'caaaaars!!!!' } '&blah' { Write-Host "It's a blah day" } '&apple' { Write-Host "I'd like to eat some apples!" } }
caaaaars!!!!

答案 6 :(得分:2)

当你需要1-liner

while( -not ( ($choice= (Read-Host "May I continue?")) -match "y|n")){ "Y or N ?"}

答案 7 :(得分:2)

这是Microsoft的documentation,用于说明如何在cmdlet中请求确认。示例在C#中,但是您也可以执行PowerShell中显示的所有操作。

首先将CmdletBinding属性添加到函数中,然后将SupportsShouldProcess设置为true。然后,您可以引用ShouldProcess变量的ShouldContinue$PSCmdlet方法。

这里是一个例子:

function Start-Work {
    <#
    .SYNOPSIS Does some work
    .PARAMETER Force
        Perform the operation without prompting for confirmation
    #>
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        # This switch allows the user to override the prompt for confirmation
        [switch]$Force
    )
    begin { }
    process {
        if ($PSCmdlet.ShouldProcess('Target')) {
            if (-not ($Force -or $PSCmdlet.ShouldContinue('Do you want to continue?', 'Caption'))) {
                return # user replied no
            }

            # Do work
        }

    }
    end { }
}

答案 8 :(得分:2)

Write-Warning "This is only a test warning." -WarningAction Inquire

来自: https://serverfault.com/a/1015583/584478

答案 9 :(得分:-1)

我更喜欢弹出窗口。

$shell = new-object -comobject "WScript.Shell"
$choice = $shell.popup("Insert question here",0,"Popup window title",4+32)

如果$ choice等于6,答案是肯定的 如果$ choice等于7,则答案为否