PowerShell for Windows Admins

Sep 25 2011   1:35PM GMT

Proxy function for Get-WmiObject

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

There are many people who don’t like to see the WMI system properties

PS> Get-WmiObject -Class Win32_OperatingSystem | select __*

__GENUS          : 2
__CLASS          : Win32_OperatingSystem
__SUPERCLASS     : CIM_OperatingSystem
__DYNASTY        : CIM_ManagedSystemElement
__RELPATH        : Win32_OperatingSystem=@
__PROPERTY_COUNT : 63
__DERIVATION     : {CIM_OperatingSystem, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER         : RSLAPTOP01
__NAMESPACE      : root\cimv2
__PATH           :
\\RSLAPTOP01\root\cimv2:Win32_OperatingSystem=@

Now one day we may get a version of Get-WmiObject that allows us to block their display but in the mean time we can create a proxy function.

Using the metaprogramming module from

http://blogs.msdn.com/powershell/archive/2009/01/04/extending-and-or-modifing-commands-with-proxies.aspx

I created a proxy function

New-ProxyCommand Get-WmiObject -CommandType All
-AddParameter NoSystemProperties > Get-WmiObject.ps1

This adds a parameter NoSystemProperties and outputs everything to a .ps1 file.

The details need to be added:

  1. turn the script into a function
  2. add the [switch] type to the property
  3. add the code to deal with the new parameter

 

if ($NoSystemProperties) {
[Void]$PSBoundParameters.Remove("NoSystemProperties")
$scriptCmd = {& $wrappedCmd @PSBoundParameters |
Select-Object -ExcludeProperty __*  -Property *|
Select-Object -ExcludeProperty Scope, Path, Options, ClassPath,
Properties, SystemProperties, Qualifiers, Site, Container -Property * }
}
else {
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
}

If the NoSystemProperties parameter is given then remove it from the bound parameters and then run get-wmiobject, exclude the system properties and then exclude the other related properties

If the NoSystemProperties isn’t set then run as normal

function Get-WmiObject {            
            
[CmdletBinding(DefaultParameterSetName='query')]            
param(            
    [Parameter(ParameterSetName='query', Mandatory=$true, Position=0)]            
    [Parameter(ParameterSetName='list', Position=1)]            
    [System.String]            
    ${Class},            
            
    [Parameter(ParameterSetName='list')]            
    [Switch]            
    ${Recurse},            
            
    [Parameter(ParameterSetName='query', Position=1)]            
    [System.String[]]            
    ${Property},            
            
    [Parameter(ParameterSetName='query')]            
    [System.String]            
    ${Filter},            
            
    [Switch]            
    ${Amended},            
            
    [Parameter(ParameterSetName='query')]            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Switch]            
    ${DirectRead},            
            
    [Parameter(ParameterSetName='list')]            
    [Switch]            
    ${List},            
            
    [Parameter(ParameterSetName='WQLQuery', Mandatory=$true)]            
    [System.String]            
    ${Query},            
            
    [Switch]            
    ${AsJob},            
            
    [Parameter(ParameterSetName='class')]            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='query')]            
    [Parameter(ParameterSetName='list')]            
    [Parameter(ParameterSetName='path')]            
    [System.Management.ImpersonationLevel]            
    ${Impersonation},            
            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='class')]            
    [Parameter(ParameterSetName='path')]            
    [Parameter(ParameterSetName='query')]            
    [Parameter(ParameterSetName='list')]            
    [System.Management.AuthenticationLevel]            
    ${Authentication},            
            
    [Parameter(ParameterSetName='class')]            
    [Parameter(ParameterSetName='list')]            
    [Parameter(ParameterSetName='path')]            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='query')]            
    [System.String]            
    ${Locale},            
            
    [Parameter(ParameterSetName='query')]            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='list')]            
    [Parameter(ParameterSetName='path')]            
    [Parameter(ParameterSetName='class')]            
    [Switch]            
    ${EnableAllPrivileges},            
            
    [Parameter(ParameterSetName='list')]            
    [Parameter(ParameterSetName='class')]            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='path')]            
    [Parameter(ParameterSetName='query')]            
    [System.String]            
    ${Authority},            
            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='path')]            
    [Parameter(ParameterSetName='class')]            
    [Parameter(ParameterSetName='query')]            
    [Parameter(ParameterSetName='list')]            
    [System.Management.Automation.PSCredential]            
    ${Credential},            
            
    [System.Int32]            
    ${ThrottleLimit},            
            
    [Parameter(ParameterSetName='list')]            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='path')]            
    [Parameter(ParameterSetName='class')]            
    [Parameter(ParameterSetName='query')]            
    [Alias('Cn')]            
    [ValidateNotNullOrEmpty()]            
    [System.String[]]            
    ${ComputerName},            
            
    [Parameter(ParameterSetName='list')]            
    [Parameter(ParameterSetName='path')]            
    [Parameter(ParameterSetName='class')]            
    [Parameter(ParameterSetName='WQLQuery')]            
    [Parameter(ParameterSetName='query')]            
    [Alias('NS')]            
    [System.String]            
    ${Namespace},            
                
    [Switch]            
    ${NoSystemProperties})            
            
begin            
{            
    try {            
        $outBuffer = $null            
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))            
        {            
            $PSBoundParameters['OutBuffer'] = 1            
        }            
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-WmiObject', [System.Management.Automation.CommandTypes]::Cmdlet)            
        if ($NoSystemProperties) {            
          [Void]$PSBoundParameters.Remove("NoSystemProperties")            
          $scriptCmd = {& $wrappedCmd @PSBoundParameters |             
             Select-Object -ExcludeProperty __*  -Property *|             
             Select-Object -ExcludeProperty Scope, Path, Options, ClassPath, Properties, SystemProperties, Qualifiers, Site, Container -Property * }            
        }            
        else {            
          $scriptCmd = {& $wrappedCmd @PSBoundParameters }            
        }              
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)            
        $steppablePipeline.Begin($PSCmdlet)            
    } catch {            
        throw            
    }            
}            
            
process            
{            
    try {            
        $steppablePipeline.Process($_)            
    } catch {            
        throw            
    }            
}            
            
end            
{            
    try {            
        $steppablePipeline.End()            
    } catch {            
        throw            
    }            
}            
            
<#

.ForwardHelpTargetName Get-WmiObject
.ForwardHelpCategory Cmdlet

#>            
            
}

The new function can be dot sourced or loaded from a module and because PowerShell runs functions before cmdlets it intercepts the cmdlet.

Get-WmiObject -Class Win32_OperatingSystem

Get-WmiObject -Class Win32_OperatingSystem –NoSystemProperties

then give us what we want.

The drawback is that using –NoSystemProperties we lose the methods but if you want them then you are either putting the results into an object or onto the pipeline in which case the system properties won’t be displayed.

Until we get a change to the cmdlet this will work.

Enjoy.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: