Windows Powershell Command In OS.execute() failing despite it working when done manually

Godot Version

4.4.1

Question

I am making a software launcher, and for that I want to check the version of a file, and since Godot doesn’t have a way to do that natively I need to make platform-specific commands, for Windows I have the following;

# 'location' is the absolute path of the executable
var cmd : String = ("get-ciminstance -Query %1Select * from CIM_DataFile WHERE Name=%2" + location.replace("\\", "\\\\") + "%2%1 | Select Version").replace("%1", '"').replace("%2", "'")

I know this command works as I can run it myself and it works fine;

(Using another software I released to test, and it gives the correct output)

But when trying to run in Godot it throws up an error for not understanding the ‘*’ for selecting all;

var out : Array[Variant]
var cmd : String = ("get-ciminstance -Query %1Select * from CIM_DataFile WHERE Name=%2" + location.replace("\\", "\\\\") + "%2%1 | Select Version").replace("%1", '"').replace("%2", "'")
print(cmd)
print(OS.execute("powershell.exe", ["-Command", cmd], out, true))
print(out)

Above prints (For this ‘location’ is the same as what I used when running the command myself);

# get-ciminstance -Query "Select * from CIM_DataFile WHERE Name='E:\\Music\\Software\\NatMusicProgramme.exe'" | Select Version
# 1
# ["
# Get-CimInstance : A positional parameter cannot be found that accepts argument '*'.
# At line:1 char:1
# + get-ciminstance -Query Select * from CIM_DataFile WHERE Name='E:\\Mus ...
# + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# + CategoryInfo          : InvalidArgument: (:) [Get-CimInstance], ParameterBindingException
# + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand
# "]

I don’t understand this as the command does work, it can accept ‘*’ as an argument when I run it, but when Godot runs it then it can’t use ‘*’ anymore? I have been at this for hours even trying to use OS.execute_with_pipe() but have got nowhere.

Thanks.

The same problem happens if you do it directly in a cmd with powershell -command ...

PS C:\> powershell.exe -command "get-ciminstance -Query `"Select * from CIM_DataFile WHERE Name='C:\\development\\godot\\Godot_v4.4-stable_win64.exe'`" | Select Version"
Get-CimInstance : A positional parameter cannot be found that accepts argument '*'.
At line:1 char:1
+ get-ciminstance -Query Select * from CIM_DataFile WHERE Name='C:\\dev ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-CimInstance], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanc
   eCommand

so it’s not a Godot issue.

Ah, thats weird that it works when ran inside powershell but when run by trying to run powershell from the command line it fails.

Though atleast I now know it isn’t a Godot issue, just powershell being weird, just have to figure out how to make the command work there now.

Thanks!