ShouldProcessメソッドで-whatifをサポートする書き込み可能なコードを試していますので、ユーザーはコマンドレットを実行する前にそれは本当のためです。Powershell:-whatifを別のモジュールのコマンドレットに伝播する方法
しかし、私はちょっとしたものに遭遇しました。 -whatifを引数としてスクリプトを呼び出すと、$ pscmdlet.ShouldProcessはfalseを返します。すべての井戸と良い。同じファイル(SupportsShouldProcess = $ true)に定義されているコマンドレットを呼び出すと、falseも返されます。
ただし、Import-Moduleを使用して読み込んだ別のモジュールで定義されているコマンドレットを呼び出すと、trueが返されます。 -whatifコンテキストは、他のモジュールの呼び出しに渡されていないようです。
すべてのコマンドレットにフラグを手動で渡す必要はありません。誰かがより良い解決策を持っていますか?
この問題はこのquestionと関連しているようです。しかし、彼らはモジュール間の問題について話しているわけではありません。
スクリプト例:
#whatiftest.ps1
[CmdletBinding(SupportsShouldProcess=$true)]
param()
Import-Module -name .\whatiftest_module -Force
function Outer
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
if($pscmdlet.ShouldProcess("Outer"))
{
Write-Host "Outer ShouldProcess"
}
else
{
Write-Host "Outer Should not Process"
}
Write-Host "Calling Inner"
Inner
Write-Host "Calling InnerModule"
InnerModule
}
function Inner
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
if($pscmdlet.ShouldProcess("Inner"))
{
Write-Host "Inner ShouldProcess"
}
else
{
Write-Host "Inner Should not Process"
}
}
Write-Host "--Normal--"
Outer
Write-Host "--WhatIf--"
Outer -WhatIf
モジュール:
#whatiftest_module.psm1
function InnerModule
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
if($pscmdlet.ShouldProcess("InnerModule"))
{
Write-Host "InnerModule ShouldProcess"
}
else
{
Write-Host "InnerModule Should not Process"
}
}
出力:
F:\temp> .\whatiftest.ps1
--Normal--
Outer ShouldProcess
Calling Inner
Inner ShouldProcess
Calling InnerModule
InnerModule ShouldProcess
--WhatIf--
What if: Performing operation "Outer" on Target "Outer".
Outer Should not Process
Calling Inner
What if: Performing operation "Inner" on Target "Inner".
Inner Should not Process
Calling InnerModule
InnerModule ShouldProcess
に役立ちます:$にWhatIf -Confirm:$ -Debugを確認してください:$デバッグ-Verbose:$ Verbose'、彼らは十字架の上で無視されます-module boundaries ... –