launching powershell script from cmake

Hi,

This question is linked with #7833.

I designed a powershell script that can take a path from CLI and add it to user path. I can run the script from a powershell or a cmd terminal but not from CMake.
I tried:

execute_process(COMMAND "powershell -File <path to script>/AddUserPath.ps1 -NewPath NewPath")
execute_process(COMMAND powershell "-File <path to script>/AddUserPath.ps1 -NewPath NewPath")
execute_process(COMMAND powershell -File <path to script>/AddUserPath.ps1 -NewPath NewPath)

or without the -File

I get only errors or nothing (for the first case).
the errors can be -File or /AddUserPath.ps1 not recognised as an applet, function or whatever.

Thanks by advance
A.

Hi,

I’m still stuck with this. Does anyone have example of powershell usage from CMake (whatever the running shell from which CMake is launched).

Thanks
A.

Hi,
I found a syntax that works, at least on windows, without space in the path:

execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)

but if the path is containing space(s) I can’t find a way to run the script:

execute_process(COMMAND powershell Myself/my path/Scripts/Test.ps1 "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:/Users/Myself/my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell Myself/my path/Scripts/Test.ps1 "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:/Users/Myself/my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell Myself\my path\Scripts\Test.ps1 "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\Myself\my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell "Myself\\my path\\Scripts\\Test.ps1" "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\Myself\my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell "\"Myself\\my path\\Scripts\\Test.ps1\"" "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ ... \Desktop\de v\CompilationFramework\Scripts\Test.ps1" -NewPath NewPath
+                                                          ~~~~~~~~
unexpected token "-NewPath"
execute_process(COMMAND powershell "\"Myself\\my path\\Scripts\\Test.ps1\" -NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ ... \Desktop\de v\CompilationFramework\Scripts\Test.ps1" -NewPath NewPath
+                                                          ~~~~~~~~
unexpected token "-NewPath"

is it a bug of execute_process?
how can I write the call properly?

Thanks
A.

Use quotes around the path to your script. CMake will then ensure the shell sees it as a single argument.

execute_process(
    COMMAND powershell
        "Myself/my path/Scripts/Test.ps1"
        "-NewPath NewPathFromCMake2"   # This looks suspicious, should it really be a single argument?
    OUTPUT_VARIABLE FOO
    RESULT_VARIABLE FOOR
    ERROR_VARIABLE  FOO_ER
)

Note that the quotes you use are not passed through to the shell command. They are interpreted by CMake as part of parsing the call to execute_process(). When you quote something like -NewPath NewPathFromCMake2, you are telling CMake to keep that together as a single argument on the command line. CMake will then apply quoting for you if needed, such as when there’s an embedded space or other special shell character.

Dear Craig,

I’m sorry but I still does not work. I tried to be a bit more exhaustive in my tests:

execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)		    SUCCESS
execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)			    SUCCESS
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)		    SUCCESS
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)		    SUCCESS
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\"" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)	    ERROR unexpected token -NewPath
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\"" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)	    ERROR unexpected token -NewPath
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1 -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)		    SUCCESS
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\" -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)	    ERROR unexpected token -NewPath

execute_process(COMMAND powershell C:/Users/Myself/my path/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)		    ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell C:/Users/Myself/my path/Scripts/Test.ps1 -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)		    ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)	    ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)  	    ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\"" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)    ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\"" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)      ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1 -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)          ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\" -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)      ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path

Yet I made experiments directly within windows shell and there is multiple issues.
the right way to call a script seems to depend on the calling shell, and a ` must be used in order to escape the whitespace:
in native shell (cmd) I found these syntaxes:

powershell C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

or

powershell - Command C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

in powershell I found two possibilities

powershell -File C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

or

C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

The easy part will be, with string(replace…) to change all space by `space.
Nevertheless, I don’t know in what shell my command is launch from CMake. I suspect that both situation may arise but I don’t know how to determine in what shell the command will be launched.
How can I do?
Or is there another syntax that will works whatever the shell?

By the way, I have another question. My script returns with EXIT and a value.
Nethertheless the returned value retrieve by execute process is always 0 when return is 0 but 1 whatever the returned value if its not 0. Is it a normal behavior?

Thanks.
A.

It seems like there’s probably some cmd /c getting in the way here (of quoting and return codes)? I don’t see it in the source though. I wonder if VERBATIM might help here too?