パラメータを明示的に渡す
-WhatIfand-Confirmパラメータを$WhatIfPreferenceand変数で渡すことができ$ConfirmPreferenceます。次の例では、パラメーター splattingを使用してこれを実現しています。
if($ConfirmPreference -eq 'Low') {$conf = @{Confirm = $true}}
StopService MyService -WhatIf:([bool]$WhatIfPreference.IsPresent) @conf
$WhatIfPreference.IsPresentスイッチが含まれている関数で使用されているTrue場合になります。含まれている関数のスイッチを-WhatIf使用すると、一時的に に設定されます。-Confirm$ConfirmPreferencelow
パラメータを暗黙的に渡す
-Confirmandは-WhatIf一時的に$ConfirmPreferenceand変数を自動的に設定するので、$WhatIfPreferenceそれらを渡す必要さえありますか?
例を考えてみましょう:
function ShouldTestCallee {
[cmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
param($test)
$PSCmdlet.ShouldProcess($env:COMPUTERNAME,"Confirm?")
}
function ShouldTestCaller {
[cmdletBinding(SupportsShouldProcess=$true)]
param($test)
ShouldTestCallee
}
$ConfirmPreference = 'High'
ShouldTestCaller
ShouldTestCaller -Confirm
ShouldTestCallerTrueからの結果ShouldProcess()
ShouldTestCaller -Confirmスイッチを渡さなかったにもかかわらず、確認プロンプトが表示されます。
編集
@manojldsの回答により、私のソリューションは常に$ConfirmPreference「低」または「高」に設定されていることに気付きました。-Confirm確認設定が「低」の場合にのみスイッチを設定するようにコードを更新しました。