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. execute powershell in batch file with QProcess

execute powershell in batch file with QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 3.5k Views
  • 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.
  • 4 Offline
    4 Offline
    411395114
    wrote on last edited by 411395114
    #1

    hi all,

    Can you help me please, to run a powershell ?

    The batch.bat runs as well as the command starts "RDC" "C: \ WINDOWS \ system32 \ mstsc.exe" but the powershell does not run.
    I tested with both the batch () methods and the powershell method below but did not launch the powershell.
    By launching the batch.bat from the command prompt, the powershell executes normally.

    project QT Widget:
    void Widget::batch()
    {
    QProcess *bat = new QProcess;
    QString path="batch.bat"";
    bat->start(path);
    bat->waitForFinished(-1);
    bat->destroyed();
    }
    // redefinition
    void Widget::batch()
    {
    QDesktopServices *QDS = new QDesktopServices();
    QString s = "../emulateur.bat";
    QDS->openUrl(QUrl::fromLocalFile(s));
    }
    void Widget::powershell() // close and reopen my widget
    {
    QProcess *ps = new QProcess;
    ps->start("../powershell.ps1");
    ps->destroyed();
    }

    batch.bat (mstsc.exe opens while the powershell does not open)
    START "RDC" "C:\WINDOWS\system32\mstsc.exe"
    powershell -executionpolicy unrestricted -file powershell.ps1

    powershell.ps1
    function RDC()
    {
    $rdc = "C:\WINDOWS\system32\mstsc.exe"
    Start-Process $rdc
    }
    RDC

    When i run the batch.bat in the cmd.exe, the powershell runs.
    Thanks in advance.
    Regards

    CP71C 1 Reply Last reply
    0
    • 4 411395114

      hi all,

      Can you help me please, to run a powershell ?

      The batch.bat runs as well as the command starts "RDC" "C: \ WINDOWS \ system32 \ mstsc.exe" but the powershell does not run.
      I tested with both the batch () methods and the powershell method below but did not launch the powershell.
      By launching the batch.bat from the command prompt, the powershell executes normally.

      project QT Widget:
      void Widget::batch()
      {
      QProcess *bat = new QProcess;
      QString path="batch.bat"";
      bat->start(path);
      bat->waitForFinished(-1);
      bat->destroyed();
      }
      // redefinition
      void Widget::batch()
      {
      QDesktopServices *QDS = new QDesktopServices();
      QString s = "../emulateur.bat";
      QDS->openUrl(QUrl::fromLocalFile(s));
      }
      void Widget::powershell() // close and reopen my widget
      {
      QProcess *ps = new QProcess;
      ps->start("../powershell.ps1");
      ps->destroyed();
      }

      batch.bat (mstsc.exe opens while the powershell does not open)
      START "RDC" "C:\WINDOWS\system32\mstsc.exe"
      powershell -executionpolicy unrestricted -file powershell.ps1

      powershell.ps1
      function RDC()
      {
      $rdc = "C:\WINDOWS\system32\mstsc.exe"
      Start-Process $rdc
      }
      RDC

      When i run the batch.bat in the cmd.exe, the powershell runs.
      Thanks in advance.
      Regards

      CP71C Offline
      CP71C Offline
      CP71
      wrote on last edited by CP71
      #2

      @411395114

      void Widget::batch()
      {
      QProcess *bat = new QProcess;
      QString path="batch.bat"";
      bat->start(path);
      }

      Hi,
      the things come to mind are:

      • You didn’t pass to QProcess full name of batch.bat ( Es: C:/Temp/batch,bat)
      • Your batch.bat isn’t in same folder of your application, if you pass only “batch.bat”

      I always prefer to pass the full name of file.

      4 1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        For powershell, this worked for me

            QProcess *process = new QProcess(this);
            process->setWorkingDirectory("e:/");
            QString cmd("powershell.exe");
            QStringList parameters{"./Demo.ps1"};
            process->start(cmd, parameters);
        

        Demo.ps1 is located in root of e:\

        1 Reply Last reply
        2
        • CP71C CP71

          @411395114

          void Widget::batch()
          {
          QProcess *bat = new QProcess;
          QString path="batch.bat"";
          bat->start(path);
          }

          Hi,
          the things come to mind are:

          • You didn’t pass to QProcess full name of batch.bat ( Es: C:/Temp/batch,bat)
          • Your batch.bat isn’t in same folder of your application, if you pass only “batch.bat”

          I always prefer to pass the full name of file.

          4 Offline
          4 Offline
          411395114
          wrote on last edited by
          #4

          @CP71
          @mrjj

          hi,
          thanks for yours answers.

          I add :
          QString directory="C:/Users/PC/Desktop/programme";
          bat->setWorkingDirectory(directory);

          My absolute path is C:/Users/PC/Desktop/program/batch.bat
          In my "program" directory, there is :

          • my widget projet (program/widget)
          • program/build-widget-Desktop... (makefile, ui_widget.h, debug and release)
          • batch.bat
          • powershell.ps1

          The batch file run but the command "powershell -executionpolicy unrestricted -file powershell.ps1" doesn't run with bat->start(path). But runs from the command prompt.

          mrjjM 1 Reply Last reply
          0
          • 4 411395114

            @CP71
            @mrjj

            hi,
            thanks for yours answers.

            I add :
            QString directory="C:/Users/PC/Desktop/programme";
            bat->setWorkingDirectory(directory);

            My absolute path is C:/Users/PC/Desktop/program/batch.bat
            In my "program" directory, there is :

            • my widget projet (program/widget)
            • program/build-widget-Desktop... (makefile, ui_widget.h, debug and release)
            • batch.bat
            • powershell.ps1

            The batch file run but the command "powershell -executionpolicy unrestricted -file powershell.ps1" doesn't run with bat->start(path). But runs from the command prompt.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @411395114
            Hi
            My best bet is its a path thing
            try with
            powershell -executionpolicy unrestricted -file c:\fullpath\powershell.ps1
            in the bat file.

            this works for me

                QProcess *process = new QProcess(this);
                QString cmd("cmd.exe");
                QStringList parameters{"/C","e:/testbat.bat"};
                process->start(cmd, parameters);
            

            Demo.ps1 is located in e:\

            bat file is

            powershell.exe -executionpolicy unrestricted -file e:\Demo.ps1
            pause
            

            and it runs. both bat and ps1 file

            1 Reply Last reply
            0
            • 4 Offline
              4 Offline
              411395114
              wrote on last edited by
              #6

              @mrjj

              The command "powershell -executionpolicy unrestricted -file c:\fullpath\powershell.ps1" open a antivirus pop up, i authorize powershell.ps1 but nothing is displayed.
              In my task manager, the widget.exe disappears and reappears in a few seconds.

              mrjjM 1 Reply Last reply
              0
              • 4 411395114

                @mrjj

                The command "powershell -executionpolicy unrestricted -file c:\fullpath\powershell.ps1" open a antivirus pop up, i authorize powershell.ps1 but nothing is displayed.
                In my task manager, the widget.exe disappears and reappears in a few seconds.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @411395114
                sounds like it runs it then :)

                What does
                powershell.ps1
                contains ?
                (something with widget.exe ?)

                Maybe scanner still block it.

                1 Reply Last reply
                0
                • 4 Offline
                  4 Offline
                  411395114
                  wrote on last edited by
                  #8

                  @CP71
                  @mrjj

                  Thanks very much for your help,
                  I retry and it's ok :
                  powershell -executionpolicy unrestricted -file "C:\Users\PC\Desktop\programme\powershell.ps1"

                  Regards

                  aha_1980A 1 Reply Last reply
                  3
                  • 4 411395114

                    @CP71
                    @mrjj

                    Thanks very much for your help,
                    I retry and it's ok :
                    powershell -executionpolicy unrestricted -file "C:\Users\PC\Desktop\programme\powershell.ps1"

                    Regards

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

                    @411395114 Just one more comment to your code:

                    The line bat->destroyed(); is senseless. destroyed() is a SIGNAL which is emitted from QProcess, nothing you are supposed to call.

                    Regards

                    Qt has to stay free or it will die.

                    4 1 Reply Last reply
                    3
                    • aha_1980A aha_1980

                      @411395114 Just one more comment to your code:

                      The line bat->destroyed(); is senseless. destroyed() is a SIGNAL which is emitted from QProcess, nothing you are supposed to call.

                      Regards

                      4 Offline
                      4 Offline
                      411395114
                      wrote on last edited by 411395114
                      #10

                      @aha_1980

                      ok, thanks for your advice.

                      Regards

                      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