Blog · Orchestration

Using the WMI Data Reader Task in SSIS for System Data Queries

Daniel Dersch · Mar 5, 2013

After discussing the Send Mail task last time, I’m going to continue the theme of workflow tasks and review WMI Data Reader.

Background:

WMI stands for Windows Management Instructions, and is an infrastructure that lets you manage data and operations on Windows systems. More simply put, WMI provides an interface for interacting with the Windows OS and hardware components. This interface can be used from C/C++, VBA, .NET, PowerShell, and SSIS.

The WMI Data Reader task enables you to use WMI in your package workflow. A common scenario for the WMI Data Reader task is to query a machine for information.

Common examples of the type of information you might retrieve with WMI include:

Data from the system’s event logsObtain the status and properties of hardware componentsThe applications, and their versions, installed on the systemRealize that WMI can go much deeper than the above examples. For instance, you can learn the capacity, type (e.g., SRAM, SDRAM, DDR, DDR-2), and operating status of physical memory in a machine. Or you can retrieve performance data about physical disks, such as how much data the disk is reading and writing per second.

Getting at this data requires using the WMI Query Language (WQL). Microsoft describes WQL as a dialect of SQL and it does look similar to T-SQL. As you’ll see in our example, a basic WQL statement can have a familiar pattern of:

SELECT FROM WHERE

With that in mind, let’s check out how to add a WMI Data Reader in BIDS and Mist.

Using BIDS:

Start by finding the WMI Data Reader task in the Toolbox.

The BIDS Toolbox with the WMI Data Reader Task selected and its tooltip

When you drag and drop the task onto the package’s design surface, you’ll see the red X icon, indicating that there are errors.

The WMI Data Reader Task on the design surface with a red X error icon

Opening the error list, you’ll see the sole error is:

The validation error saying the WQL query is missing for the WMI Data Reader Task

To solve that problem, double click on the WMI Data Reader task in the designer, to bring up its properties dialog.

The WMI Data Reader Task Editor on the WMI Options page with an empty WmiConnection and WqlQuerySource

Since BIDS is unhappy with the lack of a query, you can start by adding one. The query’s content can come from different sources; the WqlQuerySourceType property lets you select the query’s source. Its dropdown allows you to choose from:

Type    DescriptionDirect Input    Enter text directly into a propertyFile Connection Specifies a file that contains the content. The file may already exist or may be created at runtime. You might choose to store your queries in a file that you can change outside of BIDS.Variable    Create or choose a variable that contains the content.Use Direct Input and enter a query in the WqlQuerySource field. The example query below returns the free space, capacity, device ID, and description of my computer’s D drive. Naturally, you’ll want to change the drive letter to reflect the machine you’re running this query on.

The WMI Options with a WqlQuerySource query that selects the free space, size and system name of drive D

Now, based on the error list in BIDS, it would appear this is sufficient to make the task ready for use. However, if you were to press OK in the Task Editor dialog and open the error list again, you’d see:

The validation error saying no WMI connection is set for the WMI Data Reader Task

This quirk, of identifying the lack of a connection only after solving the previous error, can be annoying. Later on, I’ll show you how Mist improves on this.

In the meantime, the next step is to create a WMI connection, so the task knows where to run the query. To do that, you can use the WmiConnection property, which lets you set the target system to run the WQL query on, as well as authentication for the connection. If you click inside the WmiConnection field, you’ll see an ellipsis button. Click on the button to open a dropdown.

The WmiConnection dropdown open with the New WMI connection item

Inside the dropdown, click on ‘New WMI connection…’ to open the Connection Manager Editor dialog.

The WMI Connection Manager Editor with localhost as the server, root cimv2 as the namespace and Windows Authentication on

This dialog creates a new connection manager for the WMI data reader; the important fields are Server name and Namespace.

Server name identifies the machine to run this query on. Currently, it’s set to localhost. Since I’ll be running this task on my local machine, I’ve checked the Use Windows Authentication checkbox. If I don’t check it and try to run the task in BIDS, I’ll get an error that ‘User credentials cannot be used for local connections.’ Pressing the dialog’s Test button would also indicate the problem.

Namespace is a key field for WMI. The brief explanation is that different namespaces provide different types of functionality, known as classes. The rootcimv2 namespace (cimv2 is an abbreviation for Common Information Model v2) allows you to access most physical and logical components in a system. You can browse additional namespaces and their classes using tools such as WMI CIM Studio and Scriptomatic 2.0.

Once you’ve set up your server name and namespace, press OK in the dialog.

The next step is to choose the format of the returned WMI Data. This is specified in the Output Type property and you can choose from Data Table, Property name and value, and Property value. You can keep the default Data Table. Further down, I’ll show you how the output looks when you use the different types.

Now that you’ve entered the query, the machine to run the query on, and the format of the query results, you need to specify where the data will go.

The Destination Type property specifies if the destination is a variable or a file. For this example, keep the default and save the data to a file.

To specify the file, click inside the Destination property’s text field and click on its ellipses button. Just like the WmiConnection property, click on New Connection to open the File Connection Manager Editor.

The File Connection Manager Editor with Existing file as the usage type and an empty File box

Change the Usage type to Create file, and specify where you want the file created.

The File Connection Manager Editor with Create file as the usage type and a data.csv file in the C drive WMI Data folder

Our final step is to set the OverwriteDestination property. This property controls what happens if your destination file already has data when you run a query. You can choose to:

Type    DescriptionKeep original   Keeps the destination file’s data untouchedAppend to destination   Appends your new data to the fileOverwrite destination   Overwrite the file with your new dataUse the Keep original default.

With those steps done, the WMI Options are set as follows:

The finished WMI Options with the WMI Connection Manager, the drive D query and the data.csv destination

You can now press OK to close the dialog and return to the designer. The task’s red X icon is gone since it now has all the information it needs.

If you run this task in BIDS, by pressing F5, you should see it turn green. Furthermore, if you navigate to C:WMI Data, you should see a data.csv file. If you open it in notepad, you’ll see something like:

The data.csv output in Data table format with a header row of property names and one row of values

Note that if you try to run this task again, you’ll get an error since C:WMI Datadata.csv already exists. To prevent that, you’ll need to change the OverwriteDestination value to append or overwrite.

If you re-run this task with the ‘Property name and value’ output format, the generated file contains:

The data.csv output in Property name and value format with one name and value pair per line

If you select just Property Value, the contents are:

The data.csv output in Property value format with only the values, one per line

Using Mist:

Now let’s see how to do the above in Mist.

To start, you’ll need to create a package. If you’re unfamiliar with how to create a package, you can follow the first couple steps in the Mist User Guide’s Creating a Package topic.

Once your package is open, navigate to the Toolbox tool window.

The Mist Toolbox with the WMI Data Reader Task selected under Control Flow Tasks

Select and drag the WMI Data Reader task onto the Package’s design surface.

A new WMI Data Reader Task 1 on the Mist package design surface with a red X icon

This task has a red X icon, just like in BIDS, since it has errors.

The Mist Error List with three errors for the missing WmiConnection, Source and ConnectionName

However, you’ll notice that unlike BIDS, we list all errors upfront, along with recommendations for fixing them.

Inside the Package Details tool window, you’ll find the same properties as the WMI Data Reader dialog in BIDS.

The Mist Package Details window with the WMI Data Reader Editor and its empty Source and Destination

Our first step is to enter the WQL query. In the Source area, there is the Method dropdown, which lets you set the task’s WqlQuerySourceType property. It’s already set to the default value of Direct. Beneath the drop down is a text area, where you can enter the query. Afterwards, update the Overwrite Destination property to Overwrite.

The WMI Data Reader Editor with the drive D WQL query typed into the Source text area

The next step is to set the two connections you’ll need.

If you click on the WMI Connection dropdown, it only contains a value for (No Selection). Thus, you’ll need to add a WMI Connection to the project. You can follow the steps in the Mist User Guide’s Create New Connection section to create a connection, although you’ll want to make a WMI connection rather than an Ole DB connection.

The designer for the WMI connection has two red bordered fields, clearly indicating what you need to enter.

The Mist WmiConnection1 properties with the required Namespace and Server Name fields outlined in red

From the BIDS sample, provide an appropriate namespace and server name, and set Use Windows Authentication to true.

The WmiConnection1 required properties with the namespace root cimv2 and the server name localhost

Next, create a File connection and open its designer.

The Mist FileConnection2 properties with an empty File Path outlined in red and ExistingFile as the usage type

Matching the BIDS sample, switch the File Usage type to Create File. Clearly, you also need to enter a File Path. You can also rename this connection in case you want to use it elsewhere.

The connection renamed WMIDataCsvConnection with CreateFile as the usage type and the data.csv path in the WMI Data folder

Now, reopen the package and select the WMI Data Reader task to view its details again. In the WMI Connection combo box, you can now select WmiConnection1. In the Destination’s File Connection combo box, you can select WMIDataCsvConnection.

The finished WMI Data Reader Editor using WmiConnection1 and the WMIDataCsvConnection file connection

And that’s it. Your task is done and ready to run. To test this package, let’s run it using DTEXEC. To do that, right click on the package in the logical view.

The Mist Logical View context menu for Package1 with the Build & Run item at the top

Select the Build & Run menu item. This menu item builds the Mist package, which produces a DTSX package that DTEXEC will execute, just like in BIDS.

Next Time:

In my next post, I’ll look at a sample using the WMI Data Reader task and a related task.

Links:

WMI Data Reader documentation for the BIML language

WMI Data Reader task – MSDN

WQL Keywords

WQL Operators

WMI Win32 classes

Craig

See what BimlFlex builds.

Every layer, as native code for SSIS, Data Factory, Databricks or Snowflake.