Remote WMI connection

shlomjmi picture shlomjmi · Jul 20, 2010 · Viewed 25.5k times · Source

I want to connect to remote PC running Windows 7, from another PC using ManagementScope on a local network. On remote PC I've created a new user account "Samuel" without password and set as administrator.

ConnectionOptions options = new ConnectionOptions();
options.Username = "Samuel";
options.Password = "";

ManagementScope scope = new ManagementScope("\\\\192.168.0.2\\root\\cimv2", options);          
scope.Connect();

The Error I get:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Update:
After setting password for the use, I get new error:

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

Answer

weberph picture weberph · Jul 20, 2010

Maybe it's the missing 'EnablePrivileges':

scope.Options.EnablePrivileges = true;

From MSDN (ConnectionOptions.EnablePrivileges Property):

Gets or sets a value indicating whether user privileges need to be enabled for the connection operation. This property should only be used when the operation performed requires a certain user privilege to be enabled (for example, a machine restart).

Edit: If it doesn't work, try setting the ImpersonationLevel to 'Impersonate':

scope.Options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

ImpersonationLevel Impersonate: Impersonate-level COM impersonation level that allows objects to use the credentials of the caller. This is the recommended impersonation level for WMI calls.