There are numerous questions relating to working around the fact that PowerShell did not propagate whatif
to child functions. A comment on this answer even mentions a Connect issue that was logged, but the issue seems to have disappeared.
The issue certainly no longer exists in PowerShell 5, as this example outputs "Skipped!" as you would expect it would:
function Outer
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
Inner
}
function Inner
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
if ($PSCmdlet.ShouldProcess("Inner"))
{
Write-Host "Process!"
}
else
{
Write-Host "Skipped!"
}
}
Outer -WhatIf
However, it's not clear when this was fixed. I can't find a changelog older than 5, which makes no mention of the issue. Does anyone know which version of PowerShell fixed this issue?
(The question may seem irrelevant, but it would help script/module authors choose an appropriate minimum PS version to run against)
As the comments by @PetSerAl and @LachieWhite indicate it seems to be fixed since PowerShell 2.0.
The download link for the release notes of that version of the Windows Management Framework is unfortunately no longer working. It seems to be on Scribd, but I don't have an account. (Anyone?)
Unless you know for sure you have a lower version in production, I think it is safe to assume the switch is going to work as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With