execute powershell in batch file with QProcess
-
wrote on 23 Feb 2019, 15:19 last edited by 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.ps1powershell.ps1
function RDC()
{
$rdc = "C:\WINDOWS\system32\mstsc.exe"
Start-Process $rdc
}
RDCWhen i run the batch.bat in the cmd.exe, the powershell runs.
Thanks in advance.
Regards -
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.ps1powershell.ps1
function RDC()
{
$rdc = "C:\WINDOWS\system32\mstsc.exe"
Start-Process $rdc
}
RDCWhen i run the batch.bat in the cmd.exe, the powershell runs.
Thanks in advance.
Regardswrote on 23 Feb 2019, 18:06 last edited by CP71void 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.
-
Hi
For powershell, this worked for meQProcess *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:\
-
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.
wrote on 23 Feb 2019, 18:58 last edited byhi,
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.
-
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.
@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
-
wrote on 23 Feb 2019, 19:24 last edited by
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. -
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.@411395114
sounds like it runs it then :)What does
powershell.ps1
contains ?
(something with widget.exe ?)Maybe scanner still block it.
-
@411395114 Just one more comment to your code:
The line
bat->destroyed();
is senseless.destroyed()
is a SIGNAL which is emitted fromQProcess
, nothing you are supposed to call.Regards
-
@411395114 Just one more comment to your code:
The line
bat->destroyed();
is senseless.destroyed()
is a SIGNAL which is emitted fromQProcess
, nothing you are supposed to call.Regards
1/10