internal/functions/other/Resolve-PSEntraIDConfirmPreference.ps1
|
function Resolve-PSEntraIDConfirmPreference { <# .SYNOPSIS Resolves the effective confirmation preference for a PSEntraID cmdlet. .DESCRIPTION Evaluates the caller's Force/Confirm intent and returns a hashtable to SPLAT onto Invoke-PSFProtectedCommand. Rules: - If -Confirm was explicitly bound (regardless of value), honor it. - Else, if -Force was supplied, suppress confirmation. - Else, return an EMPTY hashtable so -Confirm is not bound at all, leaving the decision to ConfirmImpact and ConfirmPreference. The last case is why this returns a splat and not a bool. Binding -Confirm:$true does not mean "apply the usual rules" - it FORCES a prompt whatever the ConfirmImpact is. Verified with a Medium-impact command under the default ConfirmPreference of High: unbound -Confirm runs silently, -Confirm:$true prompts. The value of -Confirm is read from the bound parameters: an advanced function has no $Confirm variable (it is a common parameter), so a caller passing its $Confirm as -Confirm always passed $false and turned '-Force -Confirm:$true' into no prompt at all. -WhatIf is not affected: Invoke-PSFProtectedCommand still only previews. .PARAMETER BoundParameters The caller's $PSBoundParameters dictionary. Required to detect whether -Confirm was explicitly provided (including -Confirm:$false) and with which value. .PARAMETER Force The caller's -Force switch. .EXAMPLE PS C:\> $cmdLetConfirm = Resolve-PSEntraIDConfirmPreference -BoundParameters $PSBoundParameters -Force:$Force PS C:\> Invoke-PSFProtectedCommand -ActionString 'Some.Action' -ScriptBlock { } @cmdLetConfirm Runs the action with the confirmation the caller asked for. #> [OutputType([hashtable])] [CmdletBinding()] param( [Parameter(Mandatory = $true)] [System.Collections.IDictionary] $BoundParameters, [switch] $Force ) if ($BoundParameters.ContainsKey('Confirm')) { return @{ Confirm = [bool] $BoundParameters['Confirm'] } } if ($Force.IsPresent) { return @{ Confirm = $false } } return @{} } |