[Interest] Executing PowerShell command with quotes using QProcess
Jason Kretzer
Jason at gocodigo.com
Wed Apr 2 21:18:59 CEST 2014
Good Day Everyone,
I would like to be able to run a few certain powershell commands using QProcess. I can then read the result. Here is an example of one that works:
QProcess powershell;
powershell.setReadChannel(QProcess::StandardOutput);
powershell.start("PowerShell -Command \"& {(Get-WmiObject Win32_Videocontroller).Name}\"");
powershell.waitForReadyRead(30000);
QString allData(powershell.readAll());
powershell.close();
At the end of this, the allData has content and life is good.
However, if I try to run the exact same code but with a powershell command that has quotes in it, for example code to just get the size of the C:
QProcess powershellHDD;
powershellHDD.setReadChannel(QProcess::StandardOutput);
powershellHDD.start("PowerShell -Command \"&{(Get-WmiObject Win32_LogicalDisk -Filter \\\"DeviceID='C:’\\\").Size}\"");
powershellHDD.waitForReadyRead(30000);
QString dataHDDsize = powershellHDD.readAll();
powershellHDD.close();
This does not work as I expect it to. The dataHDDsize is an empty string “”. If I pull the command out and run it directly from a command prompt (removing the escaping backslashes) it runs exactly as I expect it to returning a number. I thought maybe the number is the problem and I have similar commands that return proper letters and the same thing occurs.
Style and suggestions for third party libs aside, does any one have any pointers?
Thanks!
-Jason
//--------------------------------//
Jason R. Kretzer
Application Developer
jason at gocodigo.com
606.792.0079
//--------------------------------//
More information about the Interest
mailing list