Can I capture the answer to the -confirm question?

Welcome Forums General PowerShell Q&A Can I capture the answer to the -confirm question?

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

5 years, 9 months ago.

  • Author
  • #4899

    Points: 0
    Rank: Member


    I have this snippet of code within the PROCESS block of my script:

    if ($PSCmdlet.ShouldProcess(
    "\n$sasexe \n-sysin ""$saspgm"" \n-config ""$sasconfig"" \n-log ""$saslogfull"" \n-print ""$sasprintfull"" \n$sasoptions","Invoke SAS"
    Try {
    if (! $quiet) {
    Print-SASResultMsg $script:sasrc
    Print-SASErrorsOrWarnings $script:sasrc
    Set-SASReturnCode $script:sasrc
    # add the return code to the array of return codes
    Catch {
    Write-Error "$_"

    (Note:  the "\n"s are really "n"s.  The crayon code chokes on the `n's, and IMO is a bug in the forum software.)

    The Invoke-SAS function uses Start-Process to invoke SAS.  The Print* functions just echo information to the console.

    If the user invokes the script like so:

    dir *.sas | myscript -confirm, or
    myscript *.sas -confirm

    They'll get something like this in the console:

    Are you sure you want to perform this action?
    Performing operation "Invoke SAS" on Target "
    D:\Program Files\SASHome\SASFoundation\9.3\sas.exe
    -sysin "R:\Jams\Jobs\Lev9\"
    -config "E:\SAS\Config\Lev9\SASApp\sasv9_JAMS_default_batch.cfg"
    -log "R:\JAMS\Logs\Lev9\_Abort.log"
    -print "R:\JAMS\Print\Lev9\_Abort.lst"
    -metaautoresources "SASApp"".
    [Y] Yes [A] Yes to All [N] No [L] No to All  Suspend [?] Help (default is "Y"):

    I thought the -confirm "checking" was done in the $PSCmdlet.ShouldProcess statement, but that doesn't appear to be the case.  I think the -confirm "checking" is done in the Start-Process cmdlet.

    The behaviour I'm seeing is, even if the user selects "[L] No to All", the Print* functions are called for all the invocations that never get executed.

    What I want is for the entire $PSCmdlet.ShouldProcess() block be skipped if the user has answered N or L to the -confirm question.

    Is there a way to make this happen?  Is this a bug in the -confirm processing (even if it's just a design bug?)


  • #5001

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    $pscmdlet.ShouldProcess() definitely triggers the confirmation prompt. I wonder if the commands you're wrapping are defining their own behavior, somehow? I see you setting ConfirmPreference to Low inside the block, so there's obviously some interaction going on, but ShouldProcess() definitely works as advertised.

    BTW, I'll look at the backtick n thing. That isn't the core forums software, it's a plugin.

  • #5011

    Points: 0
    Rank: Member

    Hi Don,

    Thanks for the reply.  Much appreciated.

    I should just admit when I make a mistake.  I had a function call outside the $pscmdlet.ShouldProcess() block, which output to the console.  Once I moved it to inside the block, I got my desired behaviour.

    Apologies for the false alarm and, again, I appreciate the time you took to reply.

    P.S.:  With this new forum software, I don't see how I mark a question as solved.

The topic ‘Can I capture the answer to the -confirm question?’ is closed to new replies.

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