How to passthru the confirm switch value?

Greetings all you awesome PowerShell-Gurus :smiley:

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:

[pre]
Main-Function – calls --> another Sub-Function – which calls --> Remove-Item.
[/pre]

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 :frowning: - Thanks for all your help!

 

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

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

[pre]

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!”
}
}

[/pre]

And the output will be…

[pre]

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!

[/pre]

 

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

 

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

<script src=“https://gist.github.com/OCram85/08cb2d3b5b9f8d19009d30de59ed4f4b.js”></script>

[pre]

~\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”):

[/pre]

… 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 :slight_smile: