DATA Step, Macro, Functions and more

Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 129
Accepted Solution

Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

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


Accepted Solutions
Solution
4 weeks ago
Frequent Contributor
Posts: 129

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

[ Edited ]
Posted in reply to kjohnsonm

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


All Replies
Super User
Posts: 23,237

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

Posted in reply to kjohnsonm

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. 

 

 

Frequent Contributor
Posts: 129

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

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...
Super User
Posts: 23,237

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

Posted in reply to kjohnsonm

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;
Frequent Contributor
Posts: 129

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

[ Edited ]

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.  

Super User
Posts: 23,237

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

Posted in reply to kjohnsonm

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 Smiley Happy

Frequent Contributor
Posts: 129

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

[ Edited ]

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. 8)   Done!

Frequent Contributor
Posts: 129

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

let me do some reading on %SYSEXEC()
Solution
4 weeks ago
Frequent Contributor
Posts: 129

Re: Is this solution still valid on Windows 10 and SAS 9.4+? Executing PowerShell script from SAS?

[ Edited ]
Posted in reply to kjohnsonm

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

 

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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