powershell PowerShell中-WhatIf和-Confirm的示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell中-WhatIf和-Confirm的示例相关的知识,希望对你有一定的参考价值。

# Usage examples
#   .\whatif.ps1 "Hello, World!"
#   .\whatif.ps1 "Hello, World!" -WhatIf
#   .\whatif.ps1 "Hello, World!" -Confirm
#   .\whatif.ps1 "Hello, World!" -Confirm:$False

# This is an example on how to use SupportsShouldProcess to implement
# the -WhatIf and -Confirm flags for your script

# Try setting the ConfirmImpact to "High" to enforce a confirmation from the
# user. This confirmation can be supressed by using the -Confirm:$False flag

[CmdletBinding(
    SupportsShouldProcess = $true,
    ConfirmImpact = "Medium"
)]

param (
    [string] $Greetings = "Hello, User"
)

$ErrorActionPreference = "Stop"

Write-Verbose ("Using greeting " -f $Greetings)

if($PSCmdlet.ShouldProcess($Greetings, "Greet user")){
    Write-Verbose "greeting the user..."
    Write-Output $Greetings
}

以上是关于powershell PowerShell中-WhatIf和-Confirm的示例的主要内容,如果未能解决你的问题,请参考以下文章