Solved How to Run PowerShell Script(.ps1) From Qt Project
-
@R_Irudezu What @SGaist has posted above is the correct way to do it.
-
@JonB I guess he wants to use startDetached
-
@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 :-)
-
@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 :) -
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 ?
-
@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
-
Yes, it finds. I tested with creating directory. It created under build of project file. So i created my powershell.script under build directory.
-
@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.
-
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();
-
@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 :)
-
@JonB i was read exit code with a function but yes, for future reading i will edit post as you said. Thanks :)