Does anyone know why querying WMI objects via c# produces partial results?

  • Thread starter Thread starter ToykoDever
  • Start date Start date
T

ToykoDever

Guest
If this is not the appropriate place to ask this question please advice a better location.

I have written a basic program that queries WMI for some objects and prints them to the console.

I am have a very strange issue with it. The query results return the correct number of objects, however only the first object in the query results has properties data set. All objects after have no property data at all set within the obj. I am hoping someone who is more knowledgeable about WMI via C# can point me in the correct direction for help.


Here is the code below.

Targeting debug Win32 .NET Framework 4.5

static void Main(string[] args)
{


ConnectionOptions options = new ConnectionOptions();

string strTargetMachine = "."; //connect to the local machine

options.Username = null; //local not needed
options.SecurePassword = null; //local not needed
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Default;
options.EnablePrivileges = true;

string strManagementScope = String.Format("\\\\{0}\\ROOT\\Microsoft\\Windows\\Storage", strTargetMachine); //for ISCSI initiator functions


ManagementScope m_storageScope = new ManagementScope(strManagementScope, options);
//m_storageScope.Connect();

string m_strQuery = "SELECT * FROM MSFT_iSCSITargetPortal";
ObjectQuery query = new ObjectQuery(m_strQuery);
ManagementObjectSearcher queryResults = new ManagementObjectSearcher(m_storageScope, query);
queryResults.Options.EnumerateDeep = true;
queryResults.Options.DirectRead = true;

foreach (ManagementObject obj in queryResults.Get())
{
Console.WriteLine("ObjectString = " + obj.ToString()); //print object as string
Console.WriteLine("TargetPortalAddess = " + obj["TargetPortalAddress"].ToString()); //print the TargetPortalAddress as a string

}


}


This results in :

ObjectString = \\HOSTNAMETEST\ROOT\Microsoft\Windows\Storage:MSFT_iSCSITargetPortal.TargetPortalAddress="1.1.1.2"
TargetPortalAddess = 1.1.1.2
ObjectString = \\HOSTNAMETEST\ROOT\Microsoft\Windows\Storage:MSFT_iSCSITargetPortal.TargetPortalAddress=""
TargetPortalAddess =


It should be

ObjectString = \\HOSTNAMETEST\ROOT\Microsoft\Windows\Storage:MSFT_iSCSITargetPortal.TargetPortalAddress="1.1.1.2"
TargetPortalAddess = 1.1.1.2
ObjectString = \\HOSTNAMETEST\ROOT\Microsoft\Windows\Storage:MSFT_iSCSITargetPortal.TargetPortalAddress="2.2.2.3"
TargetPortalAddess = 2.2.2.3

Continue reading...
 
Back
Top