Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute batch file remotely

Status
Not open for further replies.

mjjks

Programmer
Jun 22, 2005
138
US

I have a VB 6 DLL that does bunch of things.
Now I need to add functionality to execute a batch file on a server. VB 6 cannot execute command/batch files if they're not on the same machine.

This feature is important, so I was thinking to try and rewrite component in C# as I know a little bit and probably could tweak a sample code.

Anyone can advise me if that can be done fairly easily or it might involve some deep learning?

Maybe code sample or links? Thanks much.
 
I searched boards, Google and found a VB.Net sample that I converted to C#.
It uses WMI and returns success(0) with process ID, however batch file doesn't execute/code within batch file doesn't execute. Any ideas how to overcome this issue?
Thanks

========================================================
ConnectionOptions cOptions = new ConnectionOptions();
String workingDir = "D:\\FolderOnRemoteServer\\";
String mCommand = "cmd MyTestbatchFile.bat";
String servername = "\\\\NetworkServer\\";
ManagementScope scope = new ManagementScope(servername + "root\\cimv2", cOptions);

try
{
scope.Connect();
ManagementPath mp = new ManagementPath(servername + "root\\cimv2:Win32_Process");
ManagementClass mo = new ManagementClass(scope, mp, new ObjectGetOptions(null, new TimeSpan(0, 0, 0, 5), true));
ManagementBaseObject inParams = mo.GetMethodParameters("Create");
InvokeMethodOptions options = new InvokeMethodOptions();
inParams.SetPropertyValue("CommandLine", mCommand);
inParams.SetPropertyValue("CurrentDirectory",workingDir);


ManagementBaseObject mbo = mo.InvokeMethod("Create", inParams, options);
Object rv = mbo.GetPropertyValue("returnvalue");
Object prid = mbo.GetPropertyValue("processid");

}
catch (Exception e)
{
Console.WriteLine("Failed to connect: " + e.Message);
}
==========================================================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top