Passing variables using system.management?

The code below connects to a remote windows machine and starts the command line process on the remote machine.
How can I edit the code to pass commands for the command line process?

Any help would be most appreciated.

Cheers

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Management;
namespace ConsoleApplication1
{

     public class RemoteConnect
     {
          public static void Main()
          {

               //Build an options object for the connection
               ManagementClass processClass = new ManagementClass("Win32_Process");
               
               ConnectionOptions options = new ConnectionOptions();
                options.Username = "myID";
                                                 options.Password = "myPassword";

                                               //Make a connection to a remote computer using these options
                                               ManagementScope scope = new ManagementScope("\\\\MyIP\\root\\cimv2", options);
               scope.Connect();

               try
               {                    
               
                    if (scope.IsConnected)
                    {
                         Console.WriteLine("We are connected.");
                         
                    }
               }          
               catch
               {
                    Console.WriteLine("An Error Occurred:");          
               }      
               //Get an input parameters object for this method
               ManagementBaseObject inParams = null;
               processClass.Scope = scope;
               inParams = processClass.GetMethodParameters("Create");
               

               //Fill in input parameter values
               inParams["CommandLine"] = "cmd.exe";

               //Execute the method
               ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null);                        
               //Display results
               //Note: The return code of the method is provided in the "returnValue" property of the outParams object
               Console.WriteLine("Creation of the Command Line process returned: " + outParams["returnValue"]);
               Console.WriteLine(outParams.ToString());
               Console.WriteLine("Process ID: " + outParams["processId"]);
               Console.ReadLine();

          }
     }
}
LVL 1
g118481Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grayeCommented:
I probably don't understand the question...

to "add commands" to your example, you'd just replace:

               inParams["CommandLine"] = "cmd.exe";

with whatever you want to do, like:

               inParams["CommandLine"] = "C:\path\hello.exe /a";

If you're trying to run a MSDOS command (like the COPY command), then you'd need to do the following:

               inParams["CommandLine"] = "cmd.exe /c copy c:\fromdir\fromfile.txt c:\todir"

Remember that programs launched in this way run are not visible to the currently logged in user (if any).  So, for example, a command line of "calc.exe" will indeed launch the calculator application, but the current user would never it... and have no way to interact with it.   Looking at the Task Manager, you'd see that calc.exe was running (and that it is running under the credentials used to launch the original program).

Am I close?
0
grayeCommented:
I sure wish there was an edit feature...   that last next-to-the-last line was supposed to read:

                    but the current user would never SEE it
0
g118481Author Commented:
Graye,

In order to make your suggestion more clear, could you edit my code by adding yours?
0
grayeCommented:
I'm sure I don't understand...

I suggest that you replace one line of code from your example

               inParams["CommandLine"] = "cmd.exe";

with whatever you want to do, like:

               inParams["CommandLine"] = "C:\path\hello.exe /a";

If you're trying to run a MSDOS command (like the COPY command), then you'd need to do the following:

               inParams["CommandLine"] = "cmd.exe /c copy c:\fromdir\fromfile.txt c:\todir"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.