My Task Scheduler calls a Powershell script that starts a SAS program, which in turn executes a Windows command using %sysexec (and does a lot of other stuff on the side).
When I run the Powershell script interactively or start it manually in the Task Scheduler, the %sysexec command is executed correctly in the SAS program.
If the Powershell script is not started interactively by the Task Scheduler and I am not logged in on the computer, the %sysexec command is not executed. Nevertheless, it returns sysrc=0 as a result (success).
On the old machine with Windows Server 2012 it worked, on the new machine with Windows 2019 it does not.
I have written a highly simplified example here.
Task Scheduler Action:
powershell -executionpolicy bypass "C:\temp\test_sas.ps1"
Powershell Script test_sas.ps1:
$sasrootdirStr = "D:\sas\SASHome\SASFoundation\9.4"
$pgmdirStr = "C:\temp"
$pgmnameStr= "test_sas"
$logdirStr = "C:\temp"
$logfileStr = $logdirStr + "\" + $pgmnameStr + ".log"
& "$sasrootdirStr\sas.exe" "$pgmdirStr\$pgmnameStr.sas" -log "$logfileStr"
SAS program test_sas.sas:
%macro test_sas;
%local macroname;
%let macroname= test_sas;
%let newdir = %str(C:\temp\martin);
%let sysrc = -1;
%sysexec "mkdir" &newdir..;
%if &sysrc ne 0 %then %do;
%put ERROR: ¯oname: Could not execute command mkdir (sysrc: &sysrc.).;
%end;
%put sysrc: &sysrc;
%mend test_sas;
%test_sas;
Result in logfile test_sas.log (the %sysexec command mkdir was not executed):
sysrc: 0
NOTE: SAS Institute Inc, SAS Campus Drive, Cary, NC USA 27513-2414
NOTE: The SAS System used:
real time 0.60 seconds
cpu time 0.68 seconds
Used software versions:
new (with error):
old (without errors):
What is wrong?
The following code for the powershell script works. Instead of using the call operator (&), I can use the start-process comandlet. The %sysexec command in the child SAS program will then execute correctly under all circumstances, even if it is triggered in a task scheduler action.
Powershell script test_sas.ps1:
$process = (Start-Process -FilePath "$sasrootdirStr\sas.exe" -ArgumentList "`"$pgmdirStr\$pgmnameStr.sas`" -log `"$logfileStr`"" -PassThru -Wait)
Write-Host "Process terminated with return code: " $process.ExitCode