Author Posts

May 15, 2013 at 5:58 pm

Hi,

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"
))
{
$local:ConfirmPreference="Low"
Try {
Invoke-SAS
if (! $quiet) {
Print-SASResultMsg $script:sasrc
Print-SASErrorsOrWarnings $script:sasrc
}
Set-SASReturnCode $script:sasrc

# add the return code to the array of return codes
$rtncodes+=$script:sasrc
}
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:

Confirm
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\_Abort.sas"
-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?)

 

May 16, 2013 at 3:09 pm

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

May 16, 2013 at 5:16 pm

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.