[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 722
  • Last Modified:

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();

          }
     }
}
0
g118481
Asked:
g118481
  • 3
1 Solution
 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now