Summary:
Use this One Line PowerShell code to dump WMI Provider information:
gwmi -Namespace root\StifleR meta_class |
? { $_.Qualifiers['Provider'].Value } |
% { $_.getText('MOF') }
Full Story:
One of the joys of working with a fast paced Company like 2PintSoftware is watching all the cool new features getting added to our products, but that comes with
challenges when the documentation isn’t keeping pace.
One of 2Pint’s signature products is StifleR, whose job (among
other things) is to gather a whole range of datapoints about file transfers,
caches, throughput, and more! We created a WMI provider to expose some of these
datapoints. But again, even our internal notes haven’t been keeping up.
One of our team members created a shiny 44 line PowerShell
script to dump out the properties and methods for each class, and posted it to
Teams. Hey, thanks!
Microsoft Teams
However, when I tried to copy and paste the code out of our
Teams Chat window, Teams stripped out the <CR> and <LF> characters,
and everything came back in jumbled mess:
$hash.Values | Out-File "$($outDir)\$($ClassName)_Properties.csv" -Append $hash = @{
}; $filename = "$($outDir)\$($ClassName)_Methods.csv" "Type`tStatic`tName`tReturn Ty
pe`tDescription`tWrite" | Out-File $filename foreach ($prop in $class.CimClassMethod
s) { [boolean]$static = $prop.Qualifiers["static"].Value -eq $true $hash.Add($($prop
.Name), "Method`t$($static)`t$($prop.Name)`t$($prop.ReturnType)`t$($prop.Qualifiers[
"Description"].Value)`tN/A") }
Yuck!
MOF Files
So, I started manually re-adding in the newline characters. While doing this, I remembered that there is an easy way to have WMI itself dump out class
information using the MOF file format. This was great for scripting in
VBScript, VBScript doesn’t have the great formatting options of PowerShell. I
wondered if we could get WMI to output similar results for our WMI Provider.
I did some Google-Fu (I swear I didn’t use ChatGPT), and
found out about the “Meta_Class” class, a class that… well… provides
information about other classes (how very meta).
Solutions
So, starting off with the meta_class in WMI using our namespace, I filtered out only classes that were from our provider:
PS C:\ > Get-WmiObject -Namespace 'root\StifleR' -Class 'meta_class' |
where-object { $_.Qualifiers['Provider'].Value }
NameSpace:
ROOT\StifleR
Name Methods Properties
---- ------- ----------
Networks {AddNetwork, Remo... {ActiveClients, ...
Locations {ExportLocations,... {Address, Country, CountryCode, County...}
Connections {InvokePowerShell... {ActiveHours, AgentID, BandwidthAllowed, ...}
...
And then extracted the MOF definition for each class.
PS C:\ > Get-WmiObject -Namespace 'root\StifleR' -Class 'meta_class' |
where-object { $_.Qualifiers['Provider'].Value } |
foreach-Object { $_.GetText('MOF') }
...
[Description("This Class contains instances for each client connected to the StifleR server."):
ToInstance ToSubClass, dynamic: ToInstance, provider("StifleR.Service, ... )]
class ClientsRoaming : Connections
{
[implemented] string GetPhysicalDiskInformation();
[implemented] string GetPCRRSnapshot();
[implemented] string GetNetworkInfo();
[implemented] string GetCCMCacheContent();
...
The final output is about 140k of text, so a good starting point for our documentation. Hopefully you can use it too when you need to find out how a WMI class works :^)
Keith Garner