Hi, I have been trying to understand the ShouldProcess pattern and how it changes when using the verbose, debug, whatif, and confirm parameters. I'm trying to find a pattern to follow. Here is a summary of my understanding. Does this sound about right? (I assume that SupportsShouldProcess=$true.)
1. Without Verbose, Debug, WhatIf, or Confirm parameters present, the first ShouldProcess() executed checks the current value of $ConfirmPreference. If the value of $ConfirmPreference is equal to or higher than that set by ConfirmImpact, then ALL calls to ShouldProcess() within the function are subject to a confirm prompt.
2. A "yes to all" or "no to all" response to any ShouldProcess() confirm prompt suppresses confirm prompts from any additional calls to ShouldProcess() within the function.
3. Changing $ConfirmPreference after the first call has no effect on the remaining calls to ShouldProcess(). In general, all ShouldProcess() calls within a function either are or are not subject to a confirm prompt as a group.
4. If the Confirm argument is provided, $ConfirmPreference is ignored and all ShouldProcess() calls in the function are subject to a confirm prompt.
5. If the Debug argument is provided, $DebugPreference and $ConfirmPreference are ignored. All Write-Debug and ShouldProcess() calls are subject to a prompt. A "Yes to all" response to a Write-Debug prompt does not apply to more than one Write-Debug statement. (Perhaps "yes to all" should apply to all debug statements in a function? If not, then why is the option there? Bug?) "Yes to all" to a ShouldProcess() call does apply to future ShouldProcess() calls in the function. Setting $DebugPreference to a non-"Inquire" value within the function will stop confirms from Write-Debug. Passing Confirm:$false will stop confirms from ShouldProcess().
6. Using Confirm with Debug is redundant. Debug will cause ShouldProcess() to prompt as if Confirm was passed.
7. Adding Verbose does not impact the operation of Confirm or Debug.
8. Using WhatIf results in no prompt from ShouldProcess() calls, even if Confirm is present.
9. Using WhatIf with Debug results in only prompts from Write-Debug, not ShouldProcess().
10. To customize WhatIf and Confirm items (e.g., some items provide WhatIf, but not Confirm behavior), requires checking if WhatIf is present. To get only WhatIf behavior, call ShouldProcess() if WhatIf is present; otherwise, process the item anyway. (Is this a bad thing to try?)
The last item requires further explanation. I have several configurations to do using SQLPS. The changes are not committed until a few calls to alter() are made. I think using WhatIf support for uncommitted configuration changes is a nice way to display what is being done. However, I don't want a confirm for these. The final two steps that call alter() and restart services will need the added Confirm support.
When WhatIf is not used, I suppose I need to list the configuration changes. I could use Write-Verbose or Write-Host instead. It's starting to seem to me that a big string with all the changes passed to ShouldPorcess() for the final alter fits better with the normal ShouldProcess pattern, even if the string is very long. Anyway, it's probably obvious I could use some advice on a good pattern to follow here.
Thanks.
Randy in Marin