Do remote EXE calls using WMI prevent that EXE from calling other applications? - wmi

I have the following code in my C# application for running EXE’s or any other commands on a remote client. I'm trying not to rely on psexec or other tools.
public static void ConnectToRemoteClient(string client_machine, string target_exe )
{
var connection = new ConnectionOptions();
object[] theProcessToRun = { target_exe };
var wmiScope = new ManagementScope($#"\\{client_machine}\root\cimv2", connection);
wmiScope.Connect();
using (var managementClass = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
{
managementClass.InvokeMethod("Create", theProcessToRun );
}
}
A sample of how it’s used is as follows:
string exe = string.Format(#"cmd.exe /c C:\temp\Myfolder\test.bat");
ConnectToRemoteClient("ClientMachine", exe);
The test.bat just echos to a file as a test to see if the remote execute works.
Example: echo Some text > myfile.txt
It works flawlessly. However, when I use the code to launch an EXE that calls the same batch file, the EXE gets launched, but the bat file never runs.
If I double-click the EXE directly, it launches the batch file.
So my question is this. Does the WMI functionality prevent the calling of other executables or batches from the original EXE called?

ok. Figured out the issue.
I had to actually cd to the directory first and run it there. It couldn't find it if I just fed it the full path.
string exe = string.Format(#"cmd.exe /c cd C:\temp\Myfolder" + #" &RunTestbat.exe");

Related

Command window closing very quickly when running batch script with system()

I have an application developed using Borland C++Builder (Embarcadero nowadays) on Windows 10.
I want to launch a script contained in a .bat file from my app, using
the following code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
system("myfile.bat");
}
However, the command window appears then exits very fast, and I have no time to see the result.
I have added a pause command in the .bat file, but without success.
Below is the code for my .bat file:
#echo off
"./ttpmacro.exe" /I "./binary.ttl"
pause
Is there any solution to this issue?
I have resolved my issue by changing the path of the working directory. I have modified the path directory of the file "mybat.file" and then all things got OK.

system API doesn't work in an exe when called from another exe or a batch file

I have an exe, let's name it ABC.exe. There is a system API to run a batch file inside ABC.exe.
....
int ret = system("C:\\Test.bat");
if(ret == -1)
printf("The system API failed");
printf("The ret value is = %d",ret);
....
When the ABC.exe is run standalone, the Test.bat file is executed correctly. But when ABC.exe is launched via another batchfile or another application, the Test.bat is not run and the system API returns 1. I am not sure what does the return value of 1 mean here.
Please note that, the above scenario works well in Win7 but not in Win-10. And everything has been opened with Admin privileges.
Please help me in resolving the above query.
Update:
The system call fails with "dir" command as well.
I tried with other commands like "ipconfig", "winver", these are working fine.
Thanks.

Cannot require luacom library in lua script

I am new to Lua programming language. I installed Lua for Windows v5.1.5-52. I want to use luacom library to run shell script. Here is my code,
local luacom = require('luacom');
local shell = luacom.CreateObject("WScript.Shell")
shell:Run ('echo 123', 0)
which throws the following error:
lua: COM exception:(.\src\library\tLuaCOM.cpp,398):The system cannot
find the file specified.
I looked for tLuaCOM.cpp file, but could not find it, not even folder src. Though I found luacom.dll in clibs folder.
Is there any workaround with this problem?
tLuaCOM.cpp is a luacom source file, so it's probably not on your PC, except you've build it yourself.
The error comes from one of the calls - either CreateObject() or Run.
The Run Method (Windows Script Host) helps says that it starts processes:
The Run method starts a program running in a new Windows process.
but echo is a shell command, not an executable, so you have to start an instance of the Windows command interpreter and pass your command like:
shell:Run('cmd /c "echo 123"')

Windows Qt Invoke batch file and exit the App

I am developing an application in Windows8 using Qt, where I need to create an updater for it. I have Already wrote the update downloader part, and an update script to replace all the previous content with newly downloaded data.
Now I need to execute the bat file from the application itself, and exit the app before the script get executed, because the script going to remove all the dlls and application binary currently I am running.
How can I resolve this issue?
Thanks
Haris
You can use QProcess::startDetached to run an instance of command prompt with the batch file as the argument in a new process and detach from it. After that you should exit the application :
QProcess::startDetached("cmd.exe", QStringList() << "/c" << "path\\to\\mybat.bat");
qApp->quit();

Cannot launch Microsoft Management Console snap-in from Qt

I'm trying to run a program with another program. For this I use a class QProcess.
Program must be run with administrator privileges. To keep things simple step debugging and lead an example here, I started qt creator with privileges administator.
Now the fun part.
The following code runs the calculator.
QProcess * p = new QProcess();
p->start("C:\\Windows\\System32\\calc.exe");
p->waitForStarted();
delete p;
This code works.
Now another example, which already runs the service window windows.
QProcess * p = new QProcess();
p->start("C:\\Windows\\System32\\services.msc");
p->waitForStarted();
delete p;
This code does not run the program services.msc. File exists and is run from the command line without any problems.
Why one works and the other not? How to fix?
Windows 7 x86.
Short answer: .msc is not an executable file type.
Long answer:
.msc is what's called a snap-in for the Microsoft Management Console.
From the command prompt or even from Start -> Run (win + R), running services.msc tells the operating system Hey, run this file with whatever program is associated with .msc files.
That program in particular is called mmc.exe, and even when run services.msc from the command prompt and look in the Task Manager, you'll see the window actually belongs to services.exe.
Try to start either mmc.exe services.msc or cmd.exe /C services.msc instead.