When decorating a function with [CmdletBinding(SupportsShouldProcess=$true)], calling that function with -WhatIf will pass on the -WhatIf parameter to functions called within that also supports this. In the example below one can see this working for Move-Item, but not for Expand-Archive.
Steps to reproduce
function Demonstrate-Bug {
[CmdletBinding(SupportsShouldProcess=$true)]
param ()
# Does not rename file, but instead outputs the following:
# What if: Performing the operation "Move File" on target "Item: C:\Temp\Foo.zip Destination: C:\Temp\Bar.zip".
Move-Item -Path .\Foo.zip -Destination Bar.zip
# Would expect the following to not expand the archive, but instead only print a "What if" message similar to above.
Expand-Archive -Path .\Foo.zip
# If Expand-Arvhice is called explicitely with -WhatIf, I get the following output:
# What if: Performing the operation "Expand-Archive" on target "C:\Temp\Foo.zip".
}
Demonstrate-Bug -WhatIf
Expected behavior
Expand-Archive isn't actually called, but instead a message explaining what would happen without -WhatIf is outputted.
Actual behavior
The Foo.zip archive gets expanded, even though Demonstrate-Bug is called with the -WhatIf parameter.
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.16299.248
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.248
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
When decorating a function with
[CmdletBinding(SupportsShouldProcess=$true)], calling that function with-WhatIfwill pass on the-WhatIfparameter to functions called within that also supports this. In the example below one can see this working forMove-Item, but not forExpand-Archive.Steps to reproduce
Expected behavior
Expand-Archiveisn't actually called, but instead a message explaining what would happen without-WhatIfis outputted.Actual behavior
The
Foo.ziparchive gets expanded, even thoughDemonstrate-Bugis called with the-WhatIfparameter.Environment data