Hello,
I need to run some powershell code on Windows using designer systemexecute. This works fine if I get the script to a file and then run it via Powershell.exe -File "<PathToPS1>" but ideally I want to type the PS script into sysexecute using some graph parameter values and have it run from there. I couldn't get this version to work. Things I have tried:
- Set the interpreter to powershell ${}
- tried to get it through Powershell.exe "<code>" -> this works for very simple one liners
- added path as env variables etc.
The errors I am getting are usually to do with "so and so is not a recognized command" or "powershell.exe" was not found etc. I am fairly certain, I am failing to pass the code to powershell somehow and it's running off of cmd
can someone provide me with a working sample of running a multiline powershell script on windows (it could be as simple as reading a graph parameter, doing some PS math and writing to output) with cloverdesigner sysexecute so I can stop this trial/error loop?
Thank you for your help.
Pinar
-
Hi Pinar,
First off, I'd suggest using the ExecuteScript component instead of SystemExecute because ExecuteScript, in fact, represents a successor to SystemExecute. It is a well maintained and more advanced component with visual mapping options similar to other common CloverDX components.
The easiest approach to your challenge would be to prefix your command with "powershell" which is something that normally works in the default Windows shell (cmd) as well. Example script content:
The next step would be to manipulate the script content in a way to remove the carriage return and line feed white space characters (CRLF) using CTL, thus resolving the misinterpretation of new lines. In regards to the screenshot above, you can use the replace() CTL function in the Map component as follows:
$out.0.script = replace($in.0.script, "\r\n", "");
This will replace all CRLF characters with an empty string, thus effectively removing them:
The last missing piece of the puzzle would be the input mapping in ExecuteScript. You would want to configure it as shown below. All other component properties can stay at defaults.
This is what we get on the ExecuteScript success port:
{path/to/my/working/directory}>powershell -command "$a = 10; $b = 20; $sum = $a + $b; Write-Host 'The sum of numbers' $a 'and' $b 'is' $sum;"
The sum of numbers 10 and 20 is 30Regards,
-Vladi
-
This is perfect. I will try with this other component. Thank you, Pinar
Please sign in to leave a comment.
Comments 2