Use Splatting to Encapsulate WMI Calls

by Jun 4, 2014

Splatting is a great way of forwarding parameters to another cmdlet. Here is an example that can be used to encapsulate WMI calls and make them available under different names:

function Get-BIOSInfo
{
    param
    (
        $ComputerName,
        $Credential,
        $SomethingElse
    )

    $null = $PSBoundParameters.Remove('SomethingElse')

    Get-WmiObject -Class Win32_BIOS @PSBoundParameters
} 

Get-BIOSInfo gets BIOS information from WMI, and it works locally, remotely and remotely with credentials. This is possible because only the parameters that a user actually submits to Get-BIOSInfo are forwarded to the same parameters in Get-WmiObject. So when a user does not submit -Credential, then no -Credential parameter is submitted to Get-WmiObject either.

Splatting typically uses a self-defined hash table where each key represents a parameter, and each value is the argument assigned to that parameter. In this example, a predefined hash table called $PSBoundParameters is used instead. It is prefilled with the parameters submitted to the function.

Just make sure you do not forward parameters that are unknown to the destination cmdlet. To illustrate this, the function Get-BIOSInfo defines a parameter called "SomethingElse". Get-WmiObject does not have such a parameter, so before you can splat, you must call Remove() method to remove this key from the hash table.

Twitter This Tip! ReTweet this Tip!