Using Confirm only when specific parameters are specified

Welcome Forums General PowerShell Q&A Using Confirm only when specific parameters are specified

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

 
Participant
5 months, 2 weeks ago.

  • Author
    Posts
  • #96338

    Participant
    Points: 1
    Rank: Member

    I have a function that includes a parameter to delete an object if it already exists. If the object does not exist or the user does not specify the parameter to delete the object, the function has no need to be confirmed. Only if the user specifies the parameter AND the object exists should the user be prompted to confirm. In that case, I would like the user to be able to specify the common -Confirm:$false parameter to avoid the prompt. From what I've read, it seems that Confirm along with ConfirmPreference only works at the function level. Is that really the case? If so, I have some not-so-elegant ideas about how to deal with it, but I'm up for other ideas. Thanks.

  • #101094

    Participant
    Points: 56
    Rank: Member

    I would separate the tasks out into separate functions. One to get and one to remove. You can then pipe the get to the remove and have the ConfirmImpact in the cmdlet doing the change.

    The remove one should include this at the top of the function.

        [CmdletBinding(SupportsShouldProcess=$true,
                       ConfirmImpact='High')]
    
  • #101401

    Participant
    Points: 1
    Rank: Member

    Thanks, that's what I ended up doing.

The topic ‘Using Confirm only when specific parameters are specified’ is closed to new replies.