locked
How to use WMI to identify if a drive is SAN drive RRS feed

  • Question

  • Hi,
    I want to create a C# application which tells me how many drives on a machine are SAN drives. I was trying to use WMI to do this.
    Can anyone please help me with this.

    Thanks in advance.

    Friday, October 23, 2009 10:56 AM

Answers

All replies

  • Here is a code which i hope is right.

      public List<string> Inventory(string machine)
            {
                //1. Start with the Win32_DiskDrive class and query for instances of Win32_DiskPartition using the DeviceID property and the
                //Win32_DiskDriveToDiskPartition association class. Now you have a collection of the partitions on the physical drive.
                //2. Query for the Win32_LogicalDisk that represents the partition using the Win32_DiskPartition.DeviceID property and
                //Win32_LogicalDiskToPartition association class.
                //3. Get the drive letter from the Win32_LogicalDisk.DeviceID.

                List<string> info = new List<string>();
                ConnectionOptions connOptions = new ConnectionOptions();
                connOptions.Impersonation = ImpersonationLevel.Impersonate;
                connOptions.EnablePrivileges = true;
                ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", machine), connOptions);
                manScope.Connect();
                ObjectQuery oQueryDiskDrive = new ObjectQuery("select * from Win32_DiskDrive");
                ManagementObjectSearcher oSearcherDiskDrive = new ManagementObjectSearcher(manScope, oQueryDiskDrive);
                ManagementObjectCollection oReturnDiskDrive = oSearcherDiskDrive.Get();
                foreach (ManagementObject DiskDrive in oReturnDiskDrive)
                {
                    ObjectQuery oQueryDiskPartition = new ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + DiskDrive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition");
                    ManagementObjectSearcher oSearcherDiskPartition = new ManagementObjectSearcher(manScope, oQueryDiskPartition);
                    ManagementObjectCollection oReturnDiskPartition = oSearcherDiskPartition.Get();
                    foreach (ManagementObject DiskPartition in oReturnDiskPartition)
                    {
                        ObjectQuery oQueryLogicalDisk = new ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + DiskPartition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition");
                        ManagementObjectSearcher oSearcherLogicalDisk = new ManagementObjectSearcher(manScope, oQueryLogicalDisk);
                        ManagementObjectCollection oReturnLogicalDisk = oSearcherLogicalDisk.Get();
                        foreach (ManagementObject LogicalDisk in oReturnLogicalDisk)
                        {
                            try
                            {
                                info.Add("Drive Name : " + LogicalDisk["DeviceID"].ToString());
                                if (DiskDrive["PNPDeviceID"]!=null)
                                {
                                    if (DiskDrive["PNPDeviceID"].ToString().Contains("VEN_EMC"))
                                    {
                                        info.Add("EMC SAN " + DiskDrive["Model"].ToString());
                                    }
                                    if (DiskDrive["PNPDeviceID"].ToString().Contains("VEN_IBM"))
                                    {
                                        info.Add("IBM SAN " + DiskDrive["Model"].ToString());
                                    }
                                }
                                info.Add("Size : " + BytesToGB(DiskDrive["Size"].ToString()));
                                info.Add("Used Space : " + BytesToGB((Convert.ToDouble(DiskDrive["Size"].ToString()) - Convert.ToDouble(LogicalDisk["FreeSpace"].ToString())).ToString()));
                                info.Add("Free Space : " + BytesToGB(LogicalDisk["FreeSpace"].ToString()));
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }
                }
                return info;

            }

    The problem with this code is I am not sure if the DiskDrive["PNPDeviceID"] property is enough to say if the drive is SAN drive.

    Monday, October 26, 2009 4:27 AM
  • Hello SachinNair,

    Welcome to MSDN forums!

    I think you've almost found the solution, in Windows a SAN drive is considered as a normal storage which directly attached to the system, in my opinion what could do to distinct a SAN drive is by checking if the DiskDirver["Model"] matches the possible SAN models available in your enterprise. As we can see you are using "PNPDeviceID" currently as the condition, which you need to make sure all the SAN drive is registered as a PNPDevice in your solution, so I think property "Model" is a better solution.

    I'm not a specialist in SAN field, but if you need to find more information about the SAN disk you could refer to this document which provides a more rich methods to retrieve them:
    http://support.microsoft.com/kb/824190

    I've found a blog here http://blogs.msdn.com/san/archive/2009/10/22/faq-determining-if-a-drive-is-connected-via-iscsi.aspx
    I'll consulting this with my colleague, if I have any update, I'll let you konw right away.

    Here are some additional WMI classes which may address the your scenario, but I am not sure.

    http://blogs.msdn.com/san/archive/2009/08/14/mpio-wmi-classes-documentation.aspx
    http://msdn.microsoft.com/en-us/library/dd163413.aspx


    Hope this helps you.

    Thanks. 

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by SachinNair Tuesday, October 27, 2009 6:01 AM
    Monday, October 26, 2009 9:29 AM
  • Hi Tim,
    Thanks a lot for you help.
    DiskDirver["Model"]  property was the one which helped me a lot.
    I really appreciate you help.
    Tuesday, October 27, 2009 6:04 AM