Repository navigation
Closed
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-ExternalThe issue is caused by external component(s).The issue is caused by external component(s).
Description
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 -WhatIfExpected 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.1Reacted by Michael Klement
Metadata
Metadata
Assignees
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-ExternalThe issue is caused by external component(s).The issue is caused by external component(s).
Activity
mklement0 commentedon Mar 8, 2018
Good catch; it seems that
Expand-Archiveonly respects the global scope's$WhatIfPreferencevariable, not the local copy created implicitly inside the advanced function by passing-WhatIfto it.markekraus commentedon Mar 9, 2018
Hi Julian Klock (@juliank) ! can you file this issue at the https://github.com/PowerShell/Microsoft.PowerShell.Archive repo where the
Expand-Archivecode is maintained?The problem appears to be here:
https://github.com/PowerShell/Microsoft.PowerShell.Archive/blob/master/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1#L336
Rather than using
$PSCmdlet.ShouldProcess()they are checking if the-WhatIfparameter is bound.. which means it would only support-WhatIfif directly supplied to the function.juliank commentedon Mar 9, 2018
I wasn't aware of that Mark Kraus (@markekraus), thank you.
But that doesn't seem like particularly active repo. I found an issue describing this very bug, which is a whole year old, and there isn't even a single comment. Much less any work done solving it...
mklement0 commentedon Apr 4, 2018
Actually, this is ultimately a duplicate of #4568, via #6556.
While there is code that checks
$psboundparameters.ContainsKey("WhatIf")explicitly, it is in addition to proper use ofif($pscmdlet.ShouldProcess($resolvedSourcePaths))in theprocessblock.In other words: the resolution isn't external: it's a fundamental PowerShell problem.