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

  • Question

  • 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


    Wednesday, May 22, 2019 8:45 AM

Answers

  • After many many hours of tracking this issue down I will supply my own answer.

    This occurs when compiling the binary as 32bit and running it on a 64 bit OS. I am assuming there is some issue with MS running the WMI queries in WOW64 mode. 

    Running 64bit (or ANYCPU) binaries on 64 bit OSes the issue doesn't happen.

    • Marked as answer by ToykoDever Thursday, May 23, 2019 2:27 AM
    Thursday, May 23, 2019 2:27 AM

All replies

  • I do not have an iSCSI stuff so I cannot test your code but there is nothing in the WMI stuff that would cause it to randomly ignore properties. The properties are just a collection of name-value pairs so they would all come across. If we were talking about associations it would be a different story. 

    Are you sure the property is actually set on the second instance? Have you tried using WMI Code Creator or equivalent to query WMI directly to confirm. What does Powershell's Get-iScsiTargetPortal return for this?

    Can you modify the select to return just the minimal properties you care about?


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 22, 2019 2:41 PM
    Moderator
  •  In the ISCSI initiator in Windows (after enabling the service) the UI also shows all target portals correctly.

    Yes, the properties are set and if I add many portals the results are the same. If I change the order of the portal list the first one is always correct, and even though the result set returns an object for each portal setting all objects after the first are blank properties. I cannot explain it.

    Interestingly enough PowerShell has no problem returning all properties on all objects. In addition and 3rd party tool WMI Explorer also has no problem returning all properties on all objects. However try as might, admin rights, targeting different .NET Frameworks, even using different visual studio the result is always the same. The first object in the result set is correct and all properties of all other objects in the result set are "".  How can this be?

    Thursday, May 23, 2019 12:36 AM
  • After many many hours of tracking this issue down I will supply my own answer.

    This occurs when compiling the binary as 32bit and running it on a 64 bit OS. I am assuming there is some issue with MS running the WMI queries in WOW64 mode. 

    Running 64bit (or ANYCPU) binaries on 64 bit OSes the issue doesn't happen.

    • Marked as answer by ToykoDever Thursday, May 23, 2019 2:27 AM
    Thursday, May 23, 2019 2:27 AM