We help IT Professionals succeed at work.

Using System.Management.ManagementPath

bwz
bwz asked
on
1,913 Views
Last Modified: 2010-05-18
Hi,

im using System.Management for access to some machine and run a command.
the connection work and also the execute command but my problem is when
i try to run a command file from another location that not recognize in the path variable enviroment
it's cannot been done.
my problems is how to make the command to open from my input location like for example:
D:\Projects\IAI Migration\Deployment\DocSiteExport.

my relative code is:
...
...
...
path = new System.Management.ManagementPath("\\\\" + MachineName + "\\root\\cimv2:Win32_Process");
scope = new System.Management.ManagementScope(path, options);
scope.Connect();
opt = new System.Management.ObjectGetOptions();
classInstance = new System.Management.ManagementClass(scope, path, opt);
inParams = classInstance.GetMethodParameters("Create");
inParams["CommandLine"] = Command;

...
...
...

thanks for the help :)
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Leave off the ':Win32_Process' portion of the path, and select that when creating the ManagementClass instead.

Bob
bwz

Author

Commented:
hi,
you mean that i config the path location after this line:
classInstance = new System.Management.ManagementClass(scope, path, opt);      ?

i'm not sure that i got your poiint.

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Oops, I did it again :((

What is the problem that you are having opening the process on the remote machine?

I came up with this example class to try a slightly different approach:

using System;
using System.Management;

public class Win32_Process
{

    public static void RunCommand(string commandLine, string machineName, string userName, string password)
    {

        ManagementPath path = new ManagementPath(@"\\" + machineName + @"\root\cimv2:Win32_Process");

        ConnectionOptions options = new ConnectionOptions();
        options.Username = userName;
        options.Password = password;
        options.Impersonation = ImpersonationLevel.Impersonate;
        options.Authentication = AuthenticationLevel.PacketPrivacy;

        ManagementScope scope = new ManagementScope(path, options);

        ManagementClass classInstance = new ManagementClass(scope, path, new ObjectGetOptions());

        classInstance.InvokeMethod("Create", new object[] { commandLine });
    }
}

Bob
bwz

Author

Commented:
ok, thanks
i will check it on sunday, bu tell me please only one more things
what shoult i insert to path string?
can i pass this line for example ?" D:\Projects\IAI Migration\Deployment\DocSiteExport "

thanks again.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Is 'DocSiteExport' the executable?

Bob
bwz

Author

Commented:
no its the part of the path.
i'm finally run a command file the exists in that path
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
bwz

Author

Commented:
yea,
i'm actually did it and it's work (run with the full path) but i thought that there is another way.
becaue i'm got a different behaviour when i run it from the machine and run it from the c# program.
(but it's another issue i gess).
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.