How to Use -confirm in PowerShell

后端 未结 10 1396
遥遥无期
遥遥无期 2020-12-07 20:10

I\'m trying to take user input and before proceeding I would like get a message on screen and than a confirmation, whether user wants to proceed or not. I\'m using the follo

相关标签:
10条回答
  • 2020-12-07 20:40

    -Confirm is a switch in most PowerShell cmdlets that forces the cmdlet to ask for user confirmation. What you're actually looking for is the Read-Host cmdlet:

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

    or the PromptForChoice() method of the host user interface:

    $title    = '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($title, $question, $choices, 1)
    if ($decision -eq 0) {
        Write-Host 'confirmed'
    } else {
        Write-Host 'cancelled'
    }
    

    Edit:

    As M-pixel pointed out in the comments the code could be simplified further, because the choices can be passed as a simple string array.

    $title    = 'something'
    $question = 'Are you sure you want to proceed?'
    $choices  = '&Yes', '&No'
    
    $decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
    if ($decision -eq 0) {
        Write-Host 'confirmed'
    } else {
        Write-Host 'cancelled'
    }
    
    0 讨论(0)
  • 2020-12-07 20:40

    This is a simple loop that keeps prompting unless the user selects 'y' or 'n'

    $confirmation = Read-Host "Ready? [y/n]"
    while($confirmation -ne "y")
    {
        if ($confirmation -eq 'n') {exit}
        $confirmation = Read-Host "Ready? [y/n]"
    }
    
    0 讨论(0)
  • 2020-12-07 20:40

    For when you want a 1-liner

    while( -not ( ($choice= (Read-Host "May I continue?")) -match "y|n")){ "Y or N ?"}
    
    0 讨论(0)
  • 2020-12-07 20:40

    A slightly prettier function based on Ansgar Wiechers's answer. Whether it's actually more useful is a matter of debate.

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

    Example usage:

    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!!!!
    
    0 讨论(0)
  • 2020-12-07 20:41

    I prefer a popup.

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

    If $choice equals 6, the answer was Yes If $choice equals 7, the answer was No

    0 讨论(0)
  • 2020-12-07 20:48

    Here's a solution I've used, similiar to Ansgar Wiechers' solution;

    $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" }
         }
    
    0 讨论(0)
提交回复
热议问题