Getting drive and mount point information using WMI

I had to get volume and mount point information for a remote system using WMI. Although I had heard of the term “mount point” before, I had never done anything with that as my sys admin skills are not anywhere close to where my programming ones are. So after getting a quick tutorial from Google and Mikey, I was able to get going.  Basically, a mount point is when you take a drive on your machine and instead of assigning it a drive letter, you assign it a directory within an existing drive.  So instead of having C:, D:, E: drives, you would have something like C:, C:\MyMountedPoints\D, C:\MyMountedPoints\E, or something maybe a little bit friendlier.  It keeps your users from having to know the structure of your server plus you can go beyond the alphabet in number of drives.

So anyways, now the interesting part was getting the drive information.  Win32_LogicalDisk only gives you the drives that are mapped to a letter, so it doesn’t even see the mounted ones.  So I used Win32_Volume and checked for the ones that don’t have a DriveLetter.  Then to get further information about the directory, i ran a query against Win32_MountPoint and got what I needed.  I could’ve just ran the query against Win32_MountPoint but I had some data in Volume that I needed.  The tricky part was querying Win32_MountPoint since the data that it contains is of type Win32_Directory and Win32_Volume, so you have to include a bunch of escape characters to get the syntax just right…WML is very fuzzy when it comes to that and the debugging tools are really not up to par.  Here’s the code (it’s very watered down as I stripped out all the business logic that my client wouldn’t want me to post).

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string scopeStr = string.Format(@"\\{0}\root\cimv2", "TestSqlServer");

            
            ManagementScope scope = new ManagementScope(scopeStr);
            scope.Connect();

            string queryString = "SELECT * FROM Win32_Volume WHERE DriveLetter IS NULL";
            SelectQuery query = new SelectQuery(queryString);
            using (
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query))
            {
                foreach (ManagementObject disk in searcher.Get())
                {
                    string _MountPoint = disk["Name"].ToString();
                    if (_MountPoint[_MountPoint.Length - 1] == Path.DirectorySeparatorChar)
                    {
                        _MountPoint = _MountPoint.Remove(_MountPoint.Length - 1);
                    }
                    _MountPoint = _MountPoint.Replace("\\", "\\\\\\\\");

                    string _MountPointQueryString = "select * FROM Win32_MountPoint WHERE Directory=\"Win32_Directory.Name=\\\"" + _MountPoint + "\\\"\"";

                    SelectQuery _MountPointQuery = new SelectQuery(_MountPointQueryString);
                    using (
                        ManagementObjectSearcher mpsearcher =
                            new ManagementObjectSearcher(scope, _MountPointQuery))
                    {
                        foreach (ManagementObject mp in mpsearcher.Get())
                        {

                            try
                            {
                                
                                Console.WriteLine(mp["Volume"].ToString());
                                Console.WriteLine(mp["Directory"].ToString());
                                Console.WriteLine();

                            }
                            catch { }
                        }
                    }
                }
            }
        }
    }
}

About esteban

Esteban is the Founder and Chief Technologist at Nebbia Technology, an ALM consulting and Azure-powered technology company. He is a software developer with a passion for ALM, TFS, Azure, and software development best practices. Esteban is a Microsoft Visual Studio ALM MVP and ALM Ranger, Pluralsight author, and the president of ONETUG (Orlando .NET User Group).

One thought on “Error when installing SQL 2008 on Windows Server 2008 R2 (64 bit)

  1. Mark Allen

    Another approach which works well is to have the XML tag reference a custom TFS group. In my case there is a master contributor group shared between projects so rather than list all the contributor users in the template AssignedTo list boxes I created a TFS group called ‘TeamAssignments’ and within security I referenced the team. Then in the templates I added the tag ListItem value=[project]\TeamAssignments. Now when anyone is added to the team they are displayed in the AssignedTo List box. This also means that another team can be created without changing the templates as the changes can be done in security.

    TFS Team Name: Blue (example set up in security)
    TFS Group Name: TeamAssignments
    Members: Team Blue


Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? *