BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kjohnsonm
Lapis Lazuli | Level 10

Hello all,

https://communities.sas.com/t5/Base-SAS-Programming/Executing-a-powershell-script-from-SAS/td-p/2639...

 

Sorry this above post is locked I cannot add a question directly to it. But this *PowerShell script works for me perfectly from PowerShell prompt.     *Y:\SAS\Sample_Code\kmj_test.ps1

 

 

%let machine = ir53; /* Machine name with admin privileges */

x 'C:\Windows\syswow64\Windowspowershell\v1.0\powershell.exe' -file "Y:\SAS\Sample_Code\kmj_test.ps1" &machine;

My only two questions are how are you determining the machine name if that is critical or just a file handle disregard? and is this solution still valid on SAS9.4 and win10? The process just opens the x window and sits there doing nothing, no prompt, no error, no log. (I also tried with fully qualified domain PC name same results.) I also checked the path of PowerShell program that is correct, and that I have admin rights on the PC. TIA for any pointers -KJ

1 ACCEPTED SOLUTION

Accepted Solutions
kjohnsonm
Lapis Lazuli | Level 10

Ultimately I do not know if this posted original solution works or not for Win10 and newer versions of SAS (I could not get it to work for me). However I did find an acceptable method to start a PowerShell script with SAS not having to manually intervene with the script or SAS. I was going to give credit for the solution to Reeza for the idea, however Reeza declined and asked me to remove it. –KJ

The soultion I used:

/* example with hard coded path, no escape needed. */
data _null_;
CALL SYSTEM (' 
powershell Set-ExecutionPolicy -Scope CurrentUser Bypass; D:\my_folder\My_PowerShell_Script.ps1
 ');
run;

/* or 
   [The trick is simple “call system” is treating this as one command, and you need to send two, first the call to “PowerShell & the security option(s) you desire” is treated as the first command, the actual script the second command but in this case SAS will attempt to interpret the semicolon (is the PowerShell command separator) and it will fail, thus the %str(;) ]
*/

data _null_;
CALL SYSTEM (" 
powershell Set-ExecutionPolicy -Scope CurrentUser Bypass%str(;) &pname.My_PowerShell_Script.ps1
 ");
run;

Now every component is happy, System security should be happy on Windows, and PowerShell 'could' run without errors and exit cleanly back to SAS. Granted in this case the PowerShell script itself did not take any arguments but that is how I wrote the script.  -KJ

 

 

 

View solution in original post

8 REPLIES 8
Reeza
Super User

What is the string you would pass to the command line to call this program? That's what you need. And then you can use CALL SYSTEM or %SYSEXEC() to run the program. I personally don't like the X statement because its hard to get the quotes correct. 

 

 

kjohnsonm
Lapis Lazuli | Level 10
It runs without any options/input, it is a hard coded path source and searches for files like *.SAS --but in this case it's just a test program. The real one I want to use is similar in that it creates a simple csv report based on a search for files of a particular extension. Oops and I actually thought of this and just hit return to see if it would run but it does nothing also...
Reeza
Super User

for after you read:

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&docset...

 

PowerShell is probably faster than the solution above but it's an option if you need it.

 

Oh, and verify that you have XCMD set up so that you can execute these commands, since you're seeing the cmd window pop up, I think you do though. 

 

options xcmd;
kjohnsonm
Lapis Lazuli | Level 10

Reeza,

Thanks for the pointers.  I went in a little different direction based on your suggestion.

 

This works fine. Where &pname. is your path...

Note the 

%str(;)

powershell wants permission checked, so in this case it's actually two commands not one (first to set permissions, second the real comand I want to run), if you softcode your path you of course need to use double quotes and to prevent SAS from interpreting the semicolon you have to escape it...  the escaped ';' is PowerShell's join commands option --similar to && for dos or | for unix type os.

data _null_;
CALL SYSTEM (" 
powershell Set-ExecutionPolicy -Scope CurrentUser Bypass%str(;) &pname.your_script.ps1
");
run;

*or;
data _null_;
CALL SYSTEM ('powershell Set-ExecutionPolicy -Scope CurrentUser Bypass; c:\my_path\my_script.ps1');
run;
 

 

 

TIA.  

Reeza
Super User

If you can, you should change your answer to the correct solution, since that's the actual solution. My answers were really just comments or suggestions 🙂

kjohnsonm
Lapis Lazuli | Level 10

You have a point, I just wanted a real way to call any PowerShell script and get it to work reliably, and even my solution does not address why I cannot get that other form to work. But your idea sparked the thought. -KJ  PS ..and don't need anyone to look into this further. 😎   Done!

kjohnsonm
Lapis Lazuli | Level 10

Ultimately I do not know if this posted original solution works or not for Win10 and newer versions of SAS (I could not get it to work for me). However I did find an acceptable method to start a PowerShell script with SAS not having to manually intervene with the script or SAS. I was going to give credit for the solution to Reeza for the idea, however Reeza declined and asked me to remove it. –KJ

The soultion I used:

/* example with hard coded path, no escape needed. */
data _null_;
CALL SYSTEM (' 
powershell Set-ExecutionPolicy -Scope CurrentUser Bypass; D:\my_folder\My_PowerShell_Script.ps1
 ');
run;

/* or 
   [The trick is simple “call system” is treating this as one command, and you need to send two, first the call to “PowerShell & the security option(s) you desire” is treated as the first command, the actual script the second command but in this case SAS will attempt to interpret the semicolon (is the PowerShell command separator) and it will fail, thus the %str(;) ]
*/

data _null_;
CALL SYSTEM (" 
powershell Set-ExecutionPolicy -Scope CurrentUser Bypass%str(;) &pname.My_PowerShell_Script.ps1
 ");
run;

Now every component is happy, System security should be happy on Windows, and PowerShell 'could' run without errors and exit cleanly back to SAS. Granted in this case the PowerShell script itself did not take any arguments but that is how I wrote the script.  -KJ

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1878 views
  • 6 likes
  • 2 in conversation