PowerShell One-Liner to Query multiple WMI Classes with a CIM Session

Today I thought I would share a one-liner that I recently wrote to query the Manufacturer, Model, and Serial Number information from numerous remote servers. Sounds simple enough, right?

This one-liner starts out by using my New-MrCimSession function to create a CIM session to each of the specified servers. This function automatically determines if the remote server supports the WSMan protocol and falls back to DCom if it doesn’t:

Two different WMI classes  are needed to retrieve the necessary information. The manufacturer and serial number can be retrieved from the Win32_BIOS class and the model can be retrieved from the Win32_ComputerSystem class. Retrieving this information with a script or function and combining the results from both classes into a custom object would be simple, but it’s a little more challenging to retrieving this information with a one-liner.

The challenging part is reusing the CIM session with Select-Object to query the second WMI class while creating a custom object with a one-liner.

Be sure to remove those CIM sessions when you’re finished:

The CIM cmdlets were first introduced with PowerShell version 3. The computer you’re performing the query from must have at least PowerShell version 3 installed, but the remote computer that the query is being run against doesn’t need PowerShell installed at all since using Get-CimInstance with a CIM session allows it to fall back to using the DCom protocol.

The New-MrCimSession function shown in the blog article can be downloaded from my PowerShell repository on GitHub or installed as part of my MrToolkit module from the PowerShell Gallery.


Leave a Reply

%d bloggers like this: