Link to home
Start Free TrialLog in
Avatar of bwz
bwz

asked on

Using System.Management.ManagementPath

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 :)
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

Bob
Avatar of bwz
bwz

ASKER

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.

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
Avatar of bwz

ASKER

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.
Is 'DocSiteExport' the executable?

Bob
Avatar of bwz

ASKER

no its the part of the path.
i'm finally run a command file the exists in that path
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bwz

ASKER

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).