PowerShell 使用 ShouldProcess () ,带有一个参数

示例

if ($PSCmdlet.ShouldProcess("Target of action")) {
    # Do the thing
}

使用时-WhatIf:

What if: Performing the action "Invoke-MyCmdlet" 估计的正好 "Target of action"

使用时-Confirm:

Are you sure you want to perform this action?
Performing operation "Invoke-MyCmdlet" 估计的正好 "Target of action"
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):