Poorly documented code department

 

I find it amazing sometimes how hard it is to figure out how to code something so seemingly simple. I am working with my developer to create a new setup wizard for the ASP.NET Quickstart samples that will automatically download, install, and configure SQL Server Express. The recommended way to check for SQL Express is to query WMI to find out if it is installed and running, etc.

 

Well it turns out that writing .NET code to interact with WMI was not as easy as I expected. I found the System.Management namespace documentation and sample code to be pretty poor and I had to email someone on the WMI team to figure out how to make it work for me.

 

The biggest problem that I found is that the SDK documentation does not provide any simple examples of how to query for a WMI object that exists outside of the default namespace, /root/cimv2. Since I’m looking for SQL Server Express I need to look for the \root\Microsoft\SQL Server\ComputerManagement\.

 

Here is the easy way to do.

 

 

ManagementObject SQLObj1 = new ManagementObject(

         "\\root\\Microsoft\\SQLServer\\ComputerManagement",

         "SInstance.InstanceName='SQLEXPRESS'",

         null);

 

            Console.WriteLine("'SInstance.Flags > {0}", SQLObj1["NumberOfFlags"]);

           

The first parameter to the ManagementObject constructor, ManagementObject (String, String, ObjectGetOptions) is the WMI namespace that I am looking for.

 

The second parameter is the used to find a particular class object in the namespace I specified.

 

 

Okay so it took me some time to get the first part working. I could do that without the help of the WMI team, but it wasn’t as straight forward as it should have been. I’ll file a bug and get a new snippet.

 

Now for the fun stuff:

 

What I really wanted to get was the SQL Express SqlService object from the \root\Microsoft\SQLServer\ComputerManagement namespace. When I look at that the SqlService class in CimStudio I find that it has two properties that define the primary key. That means I can’t use the same query as above. I had to email the WMI team to find out that in order to define a path to a multikey property with the .NET WMI provider you need to separate the key properties with a comma. So to get what I want I had to do write the following code.

 

 

ManagementObject SQLObj2 = new ManagementObject(

                  "\\root\\Microsoft\\SQLServer\\ComputerManagement",

            "SqlService.ServiceName='MSSQL$SQLEXPRESS',SQLServiceType=1",

            null);

 

            Console.WriteLine("'SqlService.State > {0}", SQLObj2["State"]);

 

 

I’m going to try to get the docs updates to contain some details and code samples on this scenario incase anyone else can’t figure this out. In the mean time, this blog entry here to help out anyone else that is running into the same issue.

 

Now it’s time to do some more coding. I need to figure out how to use System.Management to get a list of the SqlService class objects so that I can sort through them and find the one that I am interested in. I hope that is easier to figure out.