This requirement popped up while we were helping smartdrive SD create management packs MP for SCOM. SD wanted a management pack that would capture details of running processes, count of files that were created by these processes and details pertaining to memory and CPU.

Everything that was needed was achieved, except the filecount, because WMI query language does not support the COUNT SQL function. Hence we decided to write a WMI class that would pass on this information to WMI. And the SCOM MP would pick it up from there.

1. Create a new class project in VS 2008

2. Paste the following code into the class file

using System;
using System.Collections;
using System.Management.Instrumentation;
using System.DirectoryServices;
using System.Management;
using System.Security.Principal;
using System.Diagnostics;

/Create a new namespace with the name smartdrive/

assembly: WmiConfiguration(@"root\smartdrive", HostingModel = ManagementHostingModel.LocalService)

namespace Smartdrive.WMIProvider
public class MyInstall : DefaultManagementInstaller
public override void Install(IDictionary stateSaver)
System.Runtime.InteropServices.RegistrationServices RS = new System.Runtime.InteropServices.RegistrationServices();
RS.RegisterAssembly(System.Reflection.Assembly.LoadFile(Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES%\Reference Assemblies\Microsoft\Framework\v3.5\System.Management.Instrumentation.dll")), System.Runtime.InteropServices.AssemblyRegistrationFlags.SetCodeBase); //This should be fixed with .NET 3.5 SP1
public override void Uninstall(IDictionary savedState)
try{ManagementClass MC = new ManagementClass(@"root\smartdrive:FileInfo"); /*Create a new class fileinfo in the namespace smartdrive*/
catch { }
catch { }
ManagementEntity(Name = "FileInfo")
public class FileInfo
[ManagementKey]public string FolderName { get; set; }
ManagementProbepublic string FileCount { get; set; }
ManagementProbepublic string cpuCounter { get; set; }
ManagementProbepublic string ramCounter { get; set; }

public FileInfo(string sFolderName, string sFileCount, string scpuCounter, string sramCounter)
FolderName = sFolderName;
FileCount = sFileCount;
cpuCounter = scpuCounter;
ramCounter = sramCounter;
static public IEnumerable GetFileCount()
string DirName = @"C:\";
System.IO.DirectoryInfo DirInfo = new System.IO.DirectoryInfo(DirName);
PerformanceCounter cpuCounter, ramCounter;
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");

yield return new FileInfo(DirName, DirInfo.GetFiles().Length.ToString(),cpuCounter.NextValue().ToString(),ramCounter.NextValue().ToString());


3. Build the project

4. Install the generated assembly into the GAC

5. Install the assembly as a server resource

For 4 and 5 refer this article

6. Query WMI to get the file count Use WMI code creator or WMI CIM studio from Microsoft

Last edited Oct 13, 2009 at 7:41 AM by vondino, version 1


No comments yet.