Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a command in a windows remote server and get back the console output in C# .NET

I have a remote server name (windows), username and password.

Using C# .Net, I want to run a command on the remote server and get back the console output

Is there a way to do it in C#?

I was able to run the command using WMI with the following code (partial) but with no luck of getting the console output. I could only get back the Process ID.

ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(scope, managementPath,objectGetOptions);

ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

inParams["CommandLine"] = "cmd.exe /c "+ mycommand;
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);

Any Ideas?

like image 615
BlackCursor Avatar asked Apr 10 '13 21:04

BlackCursor


People also ask

How do I run a WMIC command on a remote computer?

To create a share on a remote computer by using WMIC: At a command prompt, type wmic, and then press ENTER. Type /node:computer name where computer nameis the name of the target computer. If you want to pass administrator credentials, type /user:"domain\username", to receive a prompt for a password.

What command will run commands on a remote computer?

To run remote commands non-interactively, we use the Invoke-Command command. This command has a ComputerName parameter that allows us to specify a computer to run on the command. We also have a ScriptBlock parameter where we'll encapsulate the commands we intend to run on the remote computer.


1 Answers

This function is what I came up with after some research. Hope it helps someone else.

public string executeCommand(string serverName, string username, string password, string domain=null, string command)
{
    try
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        if (null != username)
        {
            if (null != domain)
            {
                startInfo.Arguments = "/C \"psexec.exe \\\\" + serverName + " -u " + domain+"\\"+username + " -p " + password + " " + command + "\"";
            }
            else
            {
                startInfo.Arguments = "/C \"psexec.exe \\\\" + serverName + " -u " + username + " -p " + password + " " + command + "\"";
            }
        }
        else
        {
            startInfo.Arguments = "/C \"utils\\psexec.exe "+serverName+" "+ command + "\"";
        }
        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit();

        if (process.ExitCode == 0 && null != process && process.HasExited)
        {
           return process.StandardOutput.ReadToEnd();
        }
        else
        {
            return "Error running the command : "+command;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
like image 71
BlackCursor Avatar answered Sep 20 '22 12:09

BlackCursor