PowerShell doesn't recognize cmdlet when run from own code

  • Thread starter Thread starter Kirillenseer
  • Start date Start date
K

Kirillenseer

Guest
Situation: I'm trying to zip some files from C++ which, as we all know, doesn't have any built-in support. The first solution, IShellDispatch, only works with administrative privileges (I suppose corporate IT screwed something up) and using an external library is out of question due to said corporate IT.

So I'm trying to call PowerShell to do the job for me.

The following code works fine when run from a CMD console or Run dialog

powershell -command "&{compress-archive 'InFile.1','InFile.2' OutFile.zip -force}"

I get what I expect, an OutFile.zip with both files contained. But when I run the following C++ code


wstring Parameters = L"-noexit -command \"&{compress-archive 'InFile.1','InFile.2' OutFile.zip -force}\""s;
ShellExecuteW(nullptr, nullptr, L"powershell", Parameters.c_str(), nullptr, SW_SHOWNORMAL);


I get a PowerShell telling me

The term 'compress-archive' is not recognized as the name of a cmdlet,function, script file, or operable program. Check the spelling of the name, orif a path was included, verify that the path is correct and try again.

which I don't understand. PowerShell quotes the cmdlet which is the exact same combination of letters that works when running it from CMD or Run dialog.

Continue reading...
 
Back
Top