Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Run PowerShell Script(.ps1) From Qt Project
Forum Updated to NodeBB v4.3 + New Features

How to Run PowerShell Script(.ps1) From Qt Project

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 8.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R_IrudezuR R_Irudezu

    @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...

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #4

    @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!).

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    2
    • R_IrudezuR R_Irudezu

      @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...

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @R_Irudezu What @SGaist has posted above is the correct way to do it.

      jsulmJ 1 Reply Last reply
      1
      • JonBJ JonB

        @R_Irudezu What @SGaist has posted above is the correct way to do it.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @JonB I guess he wants to use startDetached

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ 1 Reply Last reply
        0
        • jsulmJ jsulm

          @JonB I guess he wants to use startDetached

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #7

          @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) want startDetached(), 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, unlike QProcess::start().

          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

            @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) want startDetached(), 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, unlike QProcess::start().

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @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 :-)

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            JonBJ 1 Reply Last reply
            0
            • jsulmJ jsulm

              @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 :-)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #9

              @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 :)

              1 Reply Last reply
              2
              • R_IrudezuR Offline
                R_IrudezuR Offline
                R_Irudezu
                wrote on last edited by R_Irudezu
                #10

                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 :/...

                @R_Irudezu @jsulm @JonB @SGaist

                Keizoku wa chikaranari.

                R_IrudezuR 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  Did you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  aha_1980A 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Did you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @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

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    1
                    • R_IrudezuR 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 :/...

                      @R_Irudezu @jsulm @JonB @SGaist

                      R_IrudezuR Offline
                      R_IrudezuR Offline
                      R_Irudezu
                      wrote on last edited by
                      #13

                      Yes, it finds. I tested with creating directory. It created under build of project file. So i created my powershell.script under build directory.

                      Keizoku wa chikaranari.

                      JonBJ 1 Reply Last reply
                      0
                      • R_IrudezuR R_Irudezu

                        Yes, it finds. I tested with creating directory. It created under build of project file. So i created my powershell.script under build directory.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #14

                        @R_Irudezu
                        You really need to start by heeding @SGaist's post above

                        Did 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.

                        1 Reply Last reply
                        0
                        • R_IrudezuR Offline
                          R_IrudezuR Offline
                          R_Irudezu
                          wrote on last edited by R_Irudezu
                          #15

                          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();
                          

                          Keizoku wa chikaranari.

                          JonBJ 1 Reply Last reply
                          0
                          • R_IrudezuR 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();
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #16

                            @R_Irudezu
                            Good stuff, well done!

                            Move that exitCode() line down from

                                externalProcess ->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_IrudezuR 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @R_Irudezu
                              Good stuff, well done!

                              Move that exitCode() line down from

                                  externalProcess ->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_IrudezuR Offline
                              R_IrudezuR Offline
                              R_Irudezu
                              wrote on last edited by
                              #17

                              @JonB i was read exit code with a function but yes, for future reading i will edit post as you said. Thanks :)

                              Keizoku wa chikaranari.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved