Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

execute powershell in batch file with QProcess



  • 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



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


  • Lifetime Qt Champion

    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:\



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


  • Lifetime Qt Champion

    @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



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


  • Lifetime Qt Champion

    @411395114
    sounds like it runs it then :)

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

    Maybe scanner still block it.



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


  • Lifetime Qt Champion

    @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



  • @aha_1980

    ok, thanks for your advice.

    Regards


Log in to reply