Skip to content

Expand-Archive doesn't use -WhatIf parameter from calling function #6342

Closed

Description

@juliank

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

Activity

  1. mklement0 commented on Mar 8, 2018

    @mklement0
    Contributor

    Good catch; it seems that Expand-Archive only respects the global scope's $WhatIfPreference variable, not the local copy created implicitly inside the advanced function by passing -WhatIf to it.

  2. markekraus commented on Mar 9, 2018

    @markekraus
    Contributor

    Hi Julian Klock (@juliank) ! can you file this issue at the https://github.com/PowerShell/Microsoft.PowerShell.Archive repo where the Expand-Archive code 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 -WhatIf parameter is bound.. which means it would only support -WhatIf if directly supplied to the function.

  3. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    Resolution-ExternalThe issue is caused by external component(s).
    on Mar 9, 2018
  4. juliank commented on Mar 9, 2018

    @juliank
    Author

    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...

  5. mklement0 commented on Apr 4, 2018

    @mklement0
    Contributor

    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 of if($pscmdlet.ShouldProcess($resolvedSourcePaths)) in the process block.

    In other words: the resolution isn't external: it's a fundamental PowerShell problem.

  6. ghost closed this as completedon May 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally 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).

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions