如何在PowerShell中使用-confirm

use*_*612 39 powershell

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

write-host "Are you Sure You Want To Proceed:"  -Confirm
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 94

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

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

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

$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'
}
Run Code Online (Sandbox Code Playgroud)

  • 它无需显式的类型和对象声明即可工作:$ decision = $ Host.UI.PromptForChoice('something','确定要继续吗?',@('&Yes';'&No'),1)` (8认同)

小智 16

除非用户选择"y"或"n",否则这是一个保持提示的简单循环

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


Chr*_*ith 12

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

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

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

Remove-Variable小命令是示出的使用一例-confirm开关.

Remove-Variable 'reply' -Confirm
Run Code Online (Sandbox Code Playgroud)

其他参考:CommonParameters,Write-Host,Read-Host,Comparison Operators,Regular Expressions,Remove-Variable

  • 无需带出正则表达式。`-eq` 不区分大小写。`$reply -eq 'y'` 就足够了。 (3认同)

Zac*_*lin 9

这是Microsoft 提供的有关如何在 cmdlet 中请求确认的文档。这些示例使用 C#,但您也可以执行 PowerShell 中显示的所有操作。

首先将CmdletBinding属性添加到您的函数并设置SupportsShouldProcesstrue. 然后就可以引用变量的ShouldProcessShouldContinue方法了$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 { }
}
Run Code Online (Sandbox Code Playgroud)


CB.*_*CB. 8

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"
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*oll 5

当你想要一个 1-liner 时

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


小智 5

Write-Warning "This is only a test warning." -WarningAction Inquire
Run Code Online (Sandbox Code Playgroud)

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