I'm trying to execute a PowerShell command via system() command, but encounter an exception because of the spaces in the string, I tried a few things but still got the same exception.
Code:
system("powershell.exe -command Invoke-WebRequest http://example.com/myEXE.exe -OutFile C:\\Program Files\\myEXE.exe");
And this is the exception I get:
Invoke-WebRequest : A positional parameter cannot be found that accepts
argument 'Files\myEXE.exe'.
At line:1 char:1
+ Invoke-WebRequest http://example.com/myEXE.exe -OutFile C:\Program File ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
OutFile:system("powershell.exe -command Invoke-WebRequest http://example.com/myEXE.exe -OutFile 'C:\\Program Files\\myEXE.exe'");- TheoPROGRA~1to addressProgram Files. Therefore,'C:\\PROGRA~1\\myEXE.exe'should work - TobyU%appdata%) under windows. Also the proper way to run other programs (including powershell) on windows is to use CreateProcess. - GSIO01