This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
This is the first part of a serie about making a WMI help system in MSH.
this helpsystem will use the helpdescriptions that are already in WMI,
as you will see they are a bit hidden, and not always as verbose as we would want,but the are a good start, and as the are stored in the MOF files we can even change them and make your own ones (as this is way out of scope, I will not cover this here ;-))
the first part is about getting the classes and there Help (description)
this script will get all WMI classes and descriptions and put them into a HashTable for easy reference.
First interesting part is the #set Options Part
$opt.EnumerateDeep = $true
will instruct the managementclass (MC)to do a Deep enumeration, otherwise we will not get all the WMI classes back, since there not al on the same level.
the second one,
$opt.UseAmendedQualifiers = $true
is very important, because it will instruct the MC to get Amended Qualifiers also, because description is one of them .(Descriptions are stored in the MOF files, and must be retrieved)
Next are 2 booleans
$bWin32 = $true
$bPerf = $true
they will filter out the non win32_ classes and the Performancecounter classes, because most of the Time we will not want them cluttering the list, I already implemented them for making it into a switch later, so that if you need them you can get them.
next is the loop that will put hem all in a array list.
afer you run this script you can use it like this
MSH G:\Monad> $classes
Key Value
--- -----
win32_quotasetting The Win32_QuotaSetting class contains setting information for disk quotas on a volume.
win32_cdromdrive The Win32_CDROMDrive class represents a CD-ROM drive on a Win32_ComputerSystem.
win32_process The Win32_Process class represents a sequence of events on a Win32 system. Any sequence consisting of the...
...............
or like this :
MSH G:\Monad> $classes.win32_share
The Win32_Share class represents a shared resource on a Win32 system. This may be a disk drive, printer, interprocess communication, or oth
er shareable device.
Example: C:\PUBLIC.
In the next part we will dive deeper into the Class and get the Properties and Methods and again the descriptions (Yes the Methods ans Properties contain them to0) from a Single WMI Class.
the plans for the 3th part, are getting the InputParameters of Methods (and here also we have helpdescriptions again).
and then in the last part put it al together and go for the Execution ;-)
gr /\/\o\/\/
P.S.
If you use it from file / function don't forget to "Dot Source" it to get access to the HashArray.
eg:
. .\GetWmiClasses.MSH
otherwise the HashArray is out of scope (will be gone as the script ends).
# GetWmiClasses.MSH
# Gets all WMI classes and the descriptions into a HashTable
# /\/\o\/\/ 2005
$MS = new-object system.management.ManagementScope
$MC = new-object system.management.ManagementClass($MS,$NULL,$NULL)
# Set Options
$opt = new-object system.management.EnumerationOptions
$opt.EnumerateDeep = $True
$opt.UseAmendedQualifiers = $true
$classes = @{}
#new-object system.collections.arraylist
$I = 0
# Booleans to filter non win32_ classes and perfmon Classes
$bWin32 = $true
$bPerf = $true
write-host "Getting Classes"
$MC.GetSubclasses($opt) | foreach {
if ( $bWin32 -eq $true -and $bPerf -eq $true) {
$Name = $_.get_ClassPath().ClassName.ToLower()
If ( $Name.StartsWith("win32") -and !$Name.StartsWith("win32_perf")) {
[void]$Classes.add($Name,$_.get_Qualifiers()["description"].value)
Trap{[void]$Classes.add($Name,"[Empty]");continue}
}
}
ElseIf ( $bWin32 -eq $true ) {
If ( $_.ClassName.ToLower().StartsWith("win32")) {
[void]$Classes.add($_.get_ClassPath().ClassName,$_.get_Qualifiers()["description"].value)
Trap{[void]$Classes.add($Name,"[Empty]");continue}
}
}
Else {
[void]$Classes.add($_.get_ClassPath().ClassName,$_.get_Qualifiers()["description"].value)
Trap{[void]$Classes.add($Name,"[Empty]");continue}
}
#Idicate whe are busy by outputting a dot every 10 classes
If ($Classes.Count - $i -ge 10) { $i = $classes.count ; write-host -no . }
}
write-host '`nFinished use $classes[.className] to get descriptions'