Can I capture the answer to the -confirm question?


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 "<backtick>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 [S] 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?)


$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.

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.