fbpx Program: Search for WMI Classes | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Program: Search for WMI Classes

Along with WMI’s huge scope comes a related problem: finding the WMI class that accomplishes your task. If you want to dig a little deeper, though, Example 152 lets you search for WMI classes by name, description, property name, or property description.

Example 152. SearchWmiNamespace.ps1

############################################################################## ## ## SearchWmiNamespace.ps1 ##

Example 152. SearchWmiNamespace.ps1 (continued)

## Search the WMI classes installed on the system for the provided match text. ## ## ie: ## ## PS >SearchWmiNamespace Registry ## PS >SearchWmiNamespace Process ClassName,PropertyName ## PS >SearchWmiNamespace CPU Detailed ## ##############################################################################

param( [string] $pattern = $(throw "Please specify a search pattern."), [switch] $detailed, [switch] $full,

## Supports any or all of the following match options: ## ClassName, ClassDescription, PropertyName, PropertyDescription [string[]] $matchOptions = ("ClassName","ClassDescription")

)

## Helper function to create a new object that represents ## a Wmi match from this script function NewWmiMatch {

param( $matchType, $className, $propertyName, $line )

$wmiMatch = NewObject PsObject $wmiMatch | AddMember NoteProperty MatchType $matchType $wmiMatch | AddMember NoteProperty ClassName $className $wmiMatch | AddMember NoteProperty PropertyName $propertyName $wmiMatch | AddMember NoteProperty Line $line

$wmiMatch }

## If they've specified the detailed or full options, update ## the match options to provide them an appropriate amount of detail if($detailed) {

$matchOptions = "ClassName","ClassDescription","PropertyName" }

if($full) { $matchOptions = "ClassName","ClassDescription","PropertyName","PropertyDescription" }

## Verify that they specified only valid match options foreach($matchOption in $matchOptions) {

$fullMatchOptions =

Example 152. SearchWmiNamespace.ps1 (continued)

"ClassName","ClassDescription","PropertyName","PropertyDescription"

if($fullMatchOptions notcontains $matchOption) {

$error = "Cannot convert value {0} to a match option. " + "Specify one of the following values and try again. " + "The possible values are ""{1}""."

$ofs = ", " throw ($error f $matchOption, ([string] $fullMatchOptions)) } }

## Go through all of the available classes on the computer foreach($class in GetWmiObject List) {

## Provide explicit get options, so that we get back descriptions ## as well $managementOptions = NewObject System.Management.ObjectGetOptions $managementOptions.UseAmendedQualifiers = $true $managementClass =

NewObject Management.ManagementClass $class.Name,$managementOptions

## If they want us to match on class names, check if their text ## matches the class name if($matchOptions contains "ClassName") {

if($managementClass.Name match $pattern) { NewWmiMatch "ClassName" ` $managementClass.Name $null $managementClass.__PATH } }

## If they want us to match on class descriptions, check if their text ## matches the class description if($matchOptions contains "ClassDescription") {

$description = $managementClass.PsBase.Qualifiers |

foreach { if($_.Name eq "Description") { $_.Value } } if($description match $pattern) {

NewWmiMatch "ClassDescription" ` $managementClass.Name $null $description } }

## Go through the properties of the class foreach($property in $managementClass.PsBase.Properties) {

Example 152. SearchWmiNamespace.ps1 (continued)

## If they want us to match on property names, check if their text ## matches the property name if($matchOptions contains "PropertyName") {

if($property.Name match $pattern) { NewWmiMatch "PropertyName" ` $managementClass.Name $property.Name $property.Name } }

## If they want us to match on property descriptions, check if ## their text matches the property name if($matchOptions contains "PropertyDescription") {

$propertyDescription = $property.Qualifiers |

foreach { if($_.Name eq "Description") { $_.Value } } if($propertyDescription match $pattern) {

NewWmiMatch "PropertyDescription" ` $managementClass.Name $property.Name $propertyDescription } } } }

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say