How to Run PowerShell Script(.ps1) From Qt Project
-
wrote on 24 Sept 2018, 21:05 last edited by
I want to run a simple .ps1 script, but i couldn't do it. I tried to use system command like that:
system("powershell C:/Users/doe/Desktop/Demo.ps1");
I tried to use QProcess like that:
QProcess process; process.setWorkingDirectory("C:/Users/doe/Desktop"); QString cmd = ("Demo.ps1"); process.startDetached(cmd);
need help about that :/
-
Hi,
Shouldn't it be something like:
QProcess process; process.setWorkingDirectory("C:/Users/doe/Desktop"); QString cmd("powershell"); QStringList parameters{"Demo.ps1"}; process.start(cmd, parameters);
By the way, the version of
startDetached
you called is static so you're not using as you should and you won't get the result you expect. -
Hi,
Shouldn't it be something like:
QProcess process; process.setWorkingDirectory("C:/Users/doe/Desktop"); QString cmd("powershell"); QStringList parameters{"Demo.ps1"}; process.start(cmd, parameters);
By the way, the version of
startDetached
you called is static so you're not using as you should and you won't get the result you expect. -
@SGaist Hi, i'm already asking how can i do it correctly. Any other method is okay. I'm expecting an answer to how to do it...
@R_Irudezu From documentation (http://doc.qt.io/qt-5/qprocess.html#startDetached):
"Starts the program set by setProgram() with arguments set by setArguments() in a new process, and detaches from it."
So, you need to set program to execute (powershell) using setProgram() and parameters (C:/Users/doe/Desktop) using setArguments() and then call startDetached() (without parameters!). -
@SGaist Hi, i'm already asking how can i do it correctly. Any other method is okay. I'm expecting an answer to how to do it...
wrote on 25 Sept 2018, 08:00 last edited by@R_Irudezu What @SGaist has posted above is the correct way to do it.
-
@R_Irudezu What @SGaist has posted above is the correct way to do it.
@JonB I guess he wants to use startDetached
-
wrote on 25 Sept 2018, 08:07 last edited by
@jsulm
Why? I'm not a Powershell user, but I see people running Powershell commands interactively all the time. Sometimes people here say they (think they) wantstartDetached()
, but it turns out they don't!Assuming you are correct and he does, then he should follow your advice. Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is
static
, unlikeQProcess::start()
. -
@jsulm
Why? I'm not a Powershell user, but I see people running Powershell commands interactively all the time. Sometimes people here say they (think they) wantstartDetached()
, but it turns out they don't!Assuming you are correct and he does, then he should follow your advice. Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is
static
, unlikeQProcess::start()
.@JonB said in How to Run PowerShell Script(.ps1) From Qt Project:
Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is static
That's why I pointed to http://doc.qt.io/qt-5/qprocess.html#startDetached :-)
-
@JonB said in How to Run PowerShell Script(.ps1) From Qt Project:
Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is static
That's why I pointed to http://doc.qt.io/qt-5/qprocess.html#startDetached :-)
wrote on 25 Sept 2018, 08:12 last edited by JonB@jsulm
Indeed you did, and I have up-voted your answer!I was drawing OP's attention/stressing to fact of
static
, in hopeful anticipation of not having to deal with incorrect code as per https://forum.qt.io/topic/94876/qiodevice-read-qprocess-device-not-open/2 which I have been answering :) -
wrote on 25 Sept 2018, 13:11 last edited by R_Irudezu
I tried every suggestions that you said gentlemen. But nothing worked.
The script file (test.ps1) works when i execute it in the powershell window as .\test.ps1.But i want to do it in my C++ Qt program. Both system() and QProcess[start ort startDetach] are not working. I set code into a button slot function (clicked), a powershell window opening for ~0.2 second and that's it, test.ps1 wasn't executed.
Really need help about this :/...
-
Did you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?
-
Did you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?
@R_Irudezu: And to add to @SGaist,
are you sure the windows powershell can find your script? In which directory is it located? Please note that the program is usually build in an other directory than the program sources reside.
Regards
-
I tried every suggestions that you said gentlemen. But nothing worked.
The script file (test.ps1) works when i execute it in the powershell window as .\test.ps1.But i want to do it in my C++ Qt program. Both system() and QProcess[start ort startDetach] are not working. I set code into a button slot function (clicked), a powershell window opening for ~0.2 second and that's it, test.ps1 wasn't executed.
Really need help about this :/...
wrote on 25 Sept 2018, 14:55 last edited byYes, it finds. I tested with creating directory. It created under build of project file. So i created my powershell.script under build directory.
-
Yes, it finds. I tested with creating directory. It created under build of project file. So i created my powershell.script under build directory.
wrote on 25 Sept 2018, 15:06 last edited by@R_Irudezu
You really need to start by heeding @SGaist's post aboveDid you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?
You need to do follow all of the links he supplied and act on them in your code if you expect to discover what is going on.
-
wrote on 27 Sept 2018, 16:35 last edited by R_Irudezu
as @SGaist said, i checked exitCode and Errors. I solved my problem, run an .exe with a IP string parameter. Running powershell script required some permissions.
QString executer = "C:/Users/doe/Desktop/IP_Test/ip_test.exe"; QStringList params; params << "192.168.1.31"; externalProcess = new QProcess(); externalProcess ->setWorkingDirectory("C:/Users/doe/Desktop/frames"); connect(externalProcess , SIGNAL(finished(int)), this, SLOT(externalProcess (int))); connect(externalProcess , SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(aynxProcessState(QProcess::ProcessState))); externalProcess ->start(executer, params); externalProcess ->waitForFinished(-1); //wait until .exe job finished int exitCode = externalProcess->exitCode();
-
as @SGaist said, i checked exitCode and Errors. I solved my problem, run an .exe with a IP string parameter. Running powershell script required some permissions.
QString executer = "C:/Users/doe/Desktop/IP_Test/ip_test.exe"; QStringList params; params << "192.168.1.31"; externalProcess = new QProcess(); externalProcess ->setWorkingDirectory("C:/Users/doe/Desktop/frames"); connect(externalProcess , SIGNAL(finished(int)), this, SLOT(externalProcess (int))); connect(externalProcess , SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(aynxProcessState(QProcess::ProcessState))); externalProcess ->start(executer, params); externalProcess ->waitForFinished(-1); //wait until .exe job finished int exitCode = externalProcess->exitCode();
wrote on 27 Sept 2018, 18:53 last edited by@R_Irudezu
Good stuff, well done!Move that
exitCode()
line down fromexternalProcess ->exitCode(); externalProcess ->waitForFinished(-1); //wait until .exe job finished
to
externalProcess ->waitForFinished(-1); //wait until .exe job finished int exitCode = externalProcess ->exitCode();
at least for anyone future reading this :)
-
@R_Irudezu
Good stuff, well done!Move that
exitCode()
line down fromexternalProcess ->exitCode(); externalProcess ->waitForFinished(-1); //wait until .exe job finished
to
externalProcess ->waitForFinished(-1); //wait until .exe job finished int exitCode = externalProcess ->exitCode();
at least for anyone future reading this :)
1/17