How to invoke a powershell script

27 views
Skip to first unread message

Pablo Maldonado

unread,
Oct 6, 2021, 9:17:47 PM10/6/21
to schedulix
Hello,

I wanted to check if there is a possibility to invoke a powershell script.
I used the following method, but it doesn't work

powershell -command "& 'D: \ Path \ Script.ps1'"

Thank you

Ronald Jeninga

unread,
Oct 7, 2021, 4:51:45 AM10/7/21
to schedulix
Hi Pablo,

you probably need to double the backslashes. Something like

powershell -command "&D:\\Path\\Script.ps1"

You might need to experiment somewhat though.

Let me give you a little background:
In a Unix/Linux environment there is a system call execv() which is used to load a new program and to execute it. 
The basic method to spawn a new process ist to fork() first, which will create a copy of the calling process, and then to execv() the desired program.
The execv() call (it's actually an entire family of system calls) allows to specify the command line as an array of arguments.
This array remains untouched by the OS and corresponds to the argv[] array in your program.

The scheduling server parses the defined run program (according to Bourne shell rules,, replaces the parameters with their values, and creates such an argv array which is then used to create the new process.

In a Windows environment there is a CreateProcess() system call.
This system call expects a command line to execute, not an array of arguments (where argv[0] is the name of the program to execute).
Hence the job server has no other choice than to concatenate the argv array in order to create a command line that is then parsed by the Windows system.
Internally Windows creates such an argv array again (explicitly or implicitly, no idea). But this procedure easily breaks any serious quoting attempts.

It is important to note that we try to construct a Windows command line, based on the argv array, in a way that the single command line parameters are preserved.
We do this by enclosing the entries in double quotes.
Hence and array like

argv[0] = powershell
argv[1] = -command
argv[2] = & 'D: \ Path \ Script.ps1'

will be converted to 

powershell "-command" "& 'D: \ Path \ Script.ps1'"

which, due to the single quoting and the spaces, probably won't work.
In contrast, my suggestion, parsed by the scheduling server, yields

argv[0] = powershell
argv[1] = -command
argv[2] = &D:\Path\Script.ps1

which then is converted to 

powershell "-command"  "&D:\Path\Script.ps1"

which has a positive probability of being correct.

Windows users aren't out of trouble yet.
There is a difference between the treatment of bat files and exe files.
If you run a bat file that simply prints its command line arguments, you'll find that Windows preserves the quoting, while this is not true for exe files.
Hence a call like

echo.bat "hello world"

will print

"hello world"

A call like

echo.exe "hello world"

will print 

hello world

(Side note: I didn't test this behaviour for quite some time now. Things might have changed in the meanwhile). 

I hope this explanation sheds some light into the dark.

Best regards,

Ronald

Pablo Maldonado

unread,
Oct 15, 2021, 10:03:47 AM10/15/21
to schedulix

Hi Ronald,


Testing, what worked for me is to put the full path of where the powershell is 

C: :\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command 'D:\Scripts\script.ps1' PARAM1 PARAM2 

 Thank you

Ronald Jeninga

unread,
Oct 15, 2021, 10:21:49 AM10/15/21
to schedulix
Hi Pablo,

did you configure "USE_PATH" to be true for your jobserver?
If yes, then lies powershell.exe not within your PATH, if no, that could resolve your issue.

In any case I'm happy you've got it to work now.

Best regards,

Ronald

Pablo Maldonado

unread,
Oct 15, 2021, 1:05:07 PM10/15/21
to schedulix
Hi Ronald,

I set USEPATH to TRUE and perfect, the full path is no longer needed. 

 Thank you so much !!!!

Reply all
Reply to author
Forward
0 new messages