# encoding: ascii
# api: powershell
# title: Search-WmiNamespace.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: class
# author: Lee Holmes
# license: CC0
# function: New-WmiMatch
# x-poshcode-id: 2212
# x-archived: 2016-03-18T21:39:15
# x-published: 2011-09-09T21:42:00
#
#
##############################################################################
##
## Search-WmiNamespace
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Search the WMI classes installed on the system for the provided match text.
.EXAMPLE
Search-WmiNamespace Registry
Searches WMI for any classes or descriptions that mention "Registry"
.EXAMPLE
Search-WmiNamespace Process ClassName,PropertyName
Searchs WMI for any classes or properties that mention "Process"
.EXAMPLE
Search-WmiNamespace CPU -Detailed
Searches WMI for any class names, descriptions, or properties that mention
"CPU"
#>
param(
## The pattern to search for
[Parameter(Mandatory = $true)]
[string] $Pattern,
## Switch parameter to look for class names, descriptions, or properties
[switch] $Detailed,
## Switch parameter to look for class names, descriptions, properties, and
## property description.
[switch] $Full,
## Custom match options.
## Supports any or all of the following match options:
## ClassName, ClassDescription, PropertyName, PropertyDescription
[string[]] $MatchOptions = ("ClassName","ClassDescription")
)
Set-StrictMode -Off
## Helper function to create a new object that represents
## a Wmi match from this script
function New-WmiMatch
{
param( $matchType, $className, $propertyName, $line )
$wmiMatch = New-Object PsObject -Property @{
MatchType = $matchType;
ClassName = $className;
PropertyName = $propertyName;
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 =
"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 Get-WmiObject -List -Rec)
{
## Provide explicit get options, so that we get back descriptios
## as well
$managementOptions = New-Object System.Management.ObjectGetOptions
$managementOptions.UseAmendedQualifiers = $true
$managementClass =
New-Object 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)
{
New-WmiMatch "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.Qualifiers |
foreach { if($_.Name -eq "Description") { $_.Value } }
if($description -match $pattern)
{
New-WmiMatch "ClassDescription" `
$managementClass.Name $null $description
}
}
## Go through the properties of the class
foreach($property in $managementClass.Properties)
{
## 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)
{
New-WmiMatch "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)
{
New-WmiMatch "PropertyDescription" `
$managementClass.Name $property.Name $propertyDescription
}
}
}
}