0

I'd like to execute a batch file on a remote Windows server to erase log files on a computer that isn't in the same domain as me and for which I have login credentials.

I used the approach below, which worked but yielded no results.


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");
    _OutMessage = "success";
    _OutResult = true;
    _HttpStatusCode = HttpStatusCode.OK;
}
catch (Exception e)
{
    _OutMessage = e.Message.ToString();
    _OutResult = false;
    _HttpStatusCode = HttpStatusCode.InternalServerError;
}
Afzal
  • 23
  • 7

1 Answers1

0

The method I have used in the past is to enable OpenSSH on the remote machine, then use sockets to send commands to the remote and read the results back.

I would encourage you to explore the SSH.NET library (https://github.com/sshnet/SSH.NET). This could be used to solve the problem.

Trey Tomes
  • 76
  • 9