2

I want to run a command in command prompt on a remote computer using C#. Per this link How to execute a command in a remote computer?, I am trying to do this using the following code:

public static void RunRemoteCommand(string command, string RemoteMachineName)
{
    ManagementScope WMIscope = new ManagementScope(
        String.Format("\\\\{0}\\root\\cimv2", RemoteMachineName));
    WMIscope.Connect();
    ManagementClass WMIprocess = new ManagementClass(
        WMIscope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    object[] process = { command };
    object result = WMIprocess.InvokeMethod("Create", process);
    Log.Comment("Creation of process returned: " + result);
}

This returns an exit code of 0 and no errors are thrown, yet nothing is executed. Please help.

3
  • Are you aware which the Create method cannot be used to start an interactive process remotely? try checking the task manager of the remote computer. You will see the process running but without GUI. Commented Jun 14, 2013 at 0:12
  • I see what you are talking about in task manager. Is there a way to add a parameter to the process? Specifically, if I create the process "cmd.exe", is there a way to pass that process a command so it can be run on a remote machine? Commented Jun 14, 2013 at 23:07
  • Try cmd.exe /c foo.exe param1 param2 Commented Jun 14, 2013 at 23:16

3 Answers 3

1

Hope this will help

http://www.petri.co.il/command-line-wmi-part-2.htm

Have a look into "Alternate Credentials" section. i believe you are able to do the little modification yourself.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are willing to try it out, psexec is great for this sort of thing.

Comments

0

I know the post is old but for others who come across this page and also completeness: RRUZ's comment is correct , but there is also one more thing you might need in order to run on the remote machine, credentials. To do that you need to add connection options:

public static void RunRemoteCommand(string command, string RemoteMachineName, 
                                    string username,string password)
{


            var connection = new ConnectionOptions();
            connection.Username = username;
            connection.Password = password;


    ManagementScope WMIscope = new ManagementScope(
        String.Format("\\\\{0}\\root\\cimv2", RemoteMachineName), connection);
    WMIscope.Connect();
    ManagementClass WMIprocess = new ManagementClass(
        WMIscope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    object[] process = { command };
    object result = WMIprocess.InvokeMethod("Create", process);
    Log.Comment("Creation of process returned: " + result);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.