0

I'm trying to collect process information from remote machines using System.Management.

I'm impersonating an admin on the remote machine using something along these lines, but the following code throws an exception: "Access Denied".

ConnectionOptions options = new ConnectionOptions();
options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

ManagementScope scope = new ManagementScope(@"\\" + machine + @"\root\cimv2", options);
scope.Connect();

ObjectQuery query = new ObjectQuery("Select * from Win32_Process where ProcessId = "
                        + procID.ToString());

ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);

string cmdLn = "";
foreach (ManagementObject mo in mos.Get())
{
    cmdLn = (string)mo.GetPropertyValue("CommandLine");
}

However, if I supply the username and password to options, everything works fine.

I've verified that this code is executing as the impersonated context (which has sufficient permissions on the remote machine), so I'm not sure why it isn't working without the username/password passed.

Is it possible to authenticate successfully WITHOUT explicitly passing the user's credentials?

dckrooney
  • 3,041
  • 3
  • 22
  • 28

1 Answers1

0

With WMI, when impersonating a specific user you must supply explicit credentials. The link you provided above states this with the variables lpszUsername and lpszPassword.

Lizz
  • 1,442
  • 5
  • 25
  • 51
  • 1
    I am impersonating using explicit credentials. I was hoping I wouldn't need to pass these credentials a second time to use WMI (this is primarily to avoid excessive coupling between two sections of code). – dckrooney Nov 05 '12 at 18:35
  • 1
    I don't see where in the code you pass explicit creds 1st or 2nd time. Did I miss it? Either way, passing them to the remote host should be all that's needed - unless you make another WMI connection *from* that remote host to a tertiary host via WMI. WMI is difficult like that.. – Lizz Nov 06 '12 at 00:30