How to passthru the confirm switch value?

Welcome Forums General PowerShell Q&A How to passthru the confirm switch value?

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #133628

    Participant
    Points: 62
    Rank: Member

    Greetings all you awesome PowerShell-Gurus 😀

    Is there a simple way to passthru the value of the confirm switch, which comes with the 'SupportShouldProcess' attribute in advanced functions? I know how it works in general, but I never was in a situation where I have to deal with it in nested function.

    The callstack  looks like this in my specific scenario:

    Main-Function — calls –> another Sub-Function — which calls –> Remove-Item.
    

    The user itself should decide if the confirmation dialog comes up for Remove-Item, if he uses Main-Function or Main-Function -Confirm

    I think I missed something here 🙁 – Thanks for all your help!

     

  • #133638

    Moderator
    Points: 1,991
    Helping Hand
    Rank: Community Hero

    ShouldProcess behavior will be carried over to nested calls.

    '' > c:\Temp\foo.txt
    function test1{
    Remove-Item -Path c:\Temp\foo.txt
    }
    function test{
    [CmdletBinding(SupportsShouldProcess)]
    Param($r)
    test1
    }
    
    
    test -WhatIf
    test -Confirm
    
  • #133643

    Participant
    Points: 413
    Helping Hand
    Rank: Contributor

    Hi Marco – As @kvprasoon mentioned, it will be carried over to the nested calls, but you can suppress it by using '-Confirm:$false' flag.

    
    Function One-Function
    {
    [CmdLetBinding(SupportsShouldProcess=$true)]
    param()
    if($PSCmdlet.ShouldProcess($env:COMPUTERNAME,'Do nothing')){
    Two-Function -Confirm:$false
    }
    }
    
    Function Two-Function
    {
    [CmdLetBinding(SupportsShouldProcess=$true)]
    param()
    if($PSCmdlet.ShouldProcess($env:COMPUTERNAME,'Do nothing')){
    Three-Function -Confirm:$false
    }
    }
    
    Function Three-Function
    {
    [CmdLetBinding(SupportsShouldProcess=$true)]
    param()
    if($PSCmdlet.ShouldProcess($env:COMPUTERNAME,'Do nothing')){
    "Absolutely nothing!"
    }
    }
    
    

    And the output will be...

    
    PS C:\> . .\All-Functions.ps1
    PS C:\> $ConfirmPreference
    High
    PS C:\>
    PS C:\> One-Function
    Absolutely nothing!
    PS C:\>
    PS C:\>
    PS C:\> One-Function -Confirm
    
    Confirm
    Are you sure you want to perform this action?
    Performing the operation "Do nothing" on target "INSPIRE2".
    [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y
    Absolutely nothing!
    
    

     

  • #133677

    Participant
    Points: 661
    Helping Hand
    Rank: Major Contributor

    There are circumstances where it will not be carried over, most notably if you call a function from another script module inside your own script module. Binary cmdlets are not affected.

    If you need to safeguard against that, you can pass it along manually:

    function Do-WhatIfStuff {
        [CmdletBinding(SupportsShouldProcess)]
        param()
        
        Get-OtherModuleThing -Confirm:$ConfirmPreference -WhatIf:$WhatIfPreference
    }

    But in general it should pass along within the same module or from the command line to a module call. If you have issues, I'd recommend making sure all commands involved implement SupportsShouldProcess and give it another go, then just check you don't have a script function being called from another module somewhere, just in case. 🙂

     

  • #133817

    Participant
    Points: 62
    Rank: Member

    Thanks for you feedback guys. I tested it with an simple example like this:

    
    ~\workspace\lab\ConfirmTest> .\Test-ConfirmMod.ps1
    
    VERBOSE: Loading module from path '~\workspace\lab\ConfirmTest\ConfirmMod.psm1'.
    VERBOSE: Exporting function 'Invoke-MainFunc'.
    VERBOSE: Exporting function 'Invoke-SubFunc'.
    VERBOSE: Importing function 'Invoke-MainFunc'.
    VERBOSE: Importing function 'Invoke-SubFunc'.
    ============== Invoke-Mainfunc -message 'foo' ==================
    
    VERBOSE: Confirm is:
    VERBOSE: Confirm is present: False
    VERBOSE: PSBound is:
    VERBOSE: Invoke.PSBound is present: False
    VERBOSE: Invoke.PSBound is:
    VERBOSE: Sub Confirm is:
    VERBOSE: Sub Confirm is present: False
    VERBOSE: Sub PSBound is:
    VERBOSE: Sub Invoke.PSBound is present: False
    VERBOSE: Sub Invoke.PSBound is:
    
    ———– Invoke-Mainfunc -message 'foo' -Confirm ——————–
    
    VERBOSE: Confirm is:
    VERBOSE: Confirm is present: True
    VERBOSE: PSBound is: True
    VERBOSE: Invoke.PSBound is present: True
    VERBOSE: Invoke.PSBound is: True
    VERBOSE: Sub Confirm is:
    VERBOSE: Sub Confirm is present: False
    VERBOSE: Sub PSBound is:
    VERBOSE: Sub Invoke.PSBound is present: False
    VERBOSE: Sub Invoke.PSBound is:
    Confirm
    Are you sure you want to perform this action?
    Performing the operation "Output to File" on target "~\workspace\lab\ConfirmTest\foobartest.txt".
    [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Yes"):
    Confirm
    Are you sure you want to perform this action?
    Performing the operation "Remove File" on target "~\workspace\lab\ConfirmTest\foobartest.txt".
    [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Yes"):
    
    ———– Invoke-MainFunc -message 'foo' -Confirm:$false ——————–
    
    VERBOSE: Confirm is:
    VERBOSE: Confirm is present: True
    VERBOSE: PSBound is: False
    VERBOSE: Invoke.PSBound is present: True
    VERBOSE: Invoke.PSBound is: False
    VERBOSE: Sub Confirm is:
    VERBOSE: Sub Confirm is present: False
    VERBOSE: Sub PSBound is:
    VERBOSE: Sub Invoke.PSBound is present: False
    VERBOSE: Sub Invoke.PSBound is:
    
    ———– Invoke-Mainfunc -message 'foo' -Confirm:$true ——————–
    
    VERBOSE: Confirm is:
    VERBOSE: Confirm is present: True
    VERBOSE: PSBound is: True
    VERBOSE: Invoke.PSBound is present: True
    VERBOSE: Invoke.PSBound is: True
    VERBOSE: Sub Confirm is:
    VERBOSE: Sub Confirm is present: False
    VERBOSE: Sub PSBound is:
    VERBOSE: Sub Invoke.PSBound is present: False
    VERBOSE: Sub Invoke.PSBound is:
    Confirm
    Are you sure you want to perform this action?
    Performing the operation "Output to File" on target "~\workspace\lab\ConfirmTest\foobartest.txt".
    [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Yes"):
    Confirm
    Are you sure you want to perform this action?
    Performing the operation "Remove File" on target "~\workspace\lab\ConfirmTest\foobartest.txt".
    [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Yes"):
    
    

    ... And yes you were absolutely right. The Confirm switch worked like expected.

    Unfortunately only here in this lab scenario an not im my module/ function.

    If you are still interested in the actual problem you can take a look the project here

    The function New-CredentialStore -Confirm calls New-PfxCertificate. New-PfxCertificate tries to delete files and the confirm dialog doesn't appear.

    I only get a confirm dialog for actions inside New-CredentialStore itself.

    So do you have any idea what could go wrong 🙂

The topic ‘How to passthru the confirm switch value?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort