Using WMI against remote computer from a local service running as SYSTEM

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to call a WMI method on another server. This is just an example, and in this example I want to retrieve the free space on a certain drive. This is my method:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Management;

<span style="color:Blue; public <span style="color:Blue; class Test
{
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; string CalculateFreeUsed(<span style="color:Blue; string srvname, <span style="color:Blue; string drive,<span style="color:Blue; string userName, <span style="color:Blue; string passWord)
{
<span style="color:Blue; string strFreespace;
<span style="color:Blue; double D_Freespace = 0;

<span style="color:Blue; try
{
<span style="color:Green; // Connection credentials to the remote computer -
<span style="color:Green; // not needed if the logged in account has access
ConnectionOptions oConn = <span style="color:Blue; new ConnectionOptions();
oConn.Username = userName;
oConn.Password = passWord;


<span style="color:Blue; string strNameSpace = <span style="color:#A31515; @"\" + srvname + <span style="color:#A31515; @"rootcimv2";

System.Management.ManagementScope oMs = <span style="color:Blue; new System.Management.ManagementScope(strNameSpace, oConn);


<span style="color:Green; //get Fixed disk stats

System.Management.ObjectQuery oQuery = <span style="color:Blue; new System.Management.ObjectQuery(<span style="color:#A31515; "select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");

<span style="color:Green; //Execute the query
ManagementObjectSearcher oSearcher = <span style="color:Blue; new ManagementObjectSearcher(oMs, oQuery);

<span style="color:Green; //Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();

<span style="color:Green; //loop through found drives and write out info
<span style="color:Blue; foreach (ManagementObject oReturn <span style="color:Blue; in oReturnCollection)
{
<span style="color:Green; // Disk name
<span style="color:Green; //Console.WriteLine("Name : " + oReturn["Name"].ToString());
<span style="color:Green; // Free Space in bytes
<span style="color:Blue; if (drive.ToUpper() == oReturn[<span style="color:#A31515; "Name"].ToString())
{
strFreespace = oReturn[<span style="color:#A31515; "FreeSpace"].ToString();
Console.WriteLine(oReturn[<span style="color:#A31515; "Name"].ToString());
D_Freespace = D_Freespace + System.Convert.ToDouble(strFreespace);
}
}
}
<span style="color:Blue; catch (Exception ex)
{
Console.WriteLine(ex.Message);
<span style="color:Blue; return ex.Message;
}

<span style="color:Blue; return D_Freespace.ToString();
}

}
[/code]

<br/>
In the connection options I specify a user and password on the remote server (a server in my network). This code is being run from a Windows Service running as system.
I know that the actual code works as is because I can run the code when just copying it to a new project. But then, the code is running as my logged in user on my desktop.
The error I get when running from the Windows service as SYSTEM is: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
So, I tried with some impersonation. The user that I am impersonating is my local logged in user. I verified by writing some information about the WindowsIdentity in the thread that were running this code. I confirmed that the current user was my own user
and that the profile was loaded OK.
Still, I get the same error. So I wonder;
1. do I need to specify anything else in the above code (for example connection options)?
2. do I need a certain privilege when impersonating and running this code and if so, which?


View the full article
 
Back
Top