Get CIMInstance from PowerShell 2.0

I love the new CIM cmdlets in PowerShell 3.0. Querying WMI is a little faster because the CIM cmdlets query WMI using the WSMAN protocol instead of DCOM. The catch is that remote computers must be running PowerShell 3 which includes the latest version of the WSMAN protocol and the WinRM service. But if your computers are running 3.0 then you can simply run a command like this:

However, if one of the computers is running PowerShell 2.0, you’ll get an error.


In this example, CHI-DC02 is not running PowerShell 3.0. The solution is to create a CIMSession using a CIMSessionOption for DCOM.


So there is a workaround, but you have to know ahead of time which computers are not running PowerShell 3.0. When you use Get-CimInstance and specify a computername, the cmdlet setups up a temporary CIMSession. So why not create the temporary CIMSession with the DCOM option if it is needed? So I wrote a “wrapper” function called Get-MyCimInstance to do just that.

The heart of the function is a nested function to test if a remote computer is running WSMAN 3.0.

The function uses Test-WSMan and a regular expression to get the remoting version. If it is 3.0 the function returns True. In Get-MyCIMInstance I test each computer and if not running 3.0, create the CIMSession option and include it when creating the temporary CIMSession.

I’m using a Try/Catch block because if the computer is offline, my test function will throw an exception which I can catch.

Otherwise, all is good and  I can pass the rest of the parameters to Get-CimInstance.

At the end of the process, I remove the temporary CIMSession. With this, now I can query both v2 and v3 computers.
Notice for CHI-DC02 I’m creating the DCOM option. Here’s the command without all the verboseness.
I could have created a proxy function for Get-CimInstance, but not only are they more complicated, I didn’t want that much transparency. I wanted to know that I’m querying using my function and not Get-CimInstance. Here’s the complete script.

I hope you’ll let me know what you think and if you find this useful.

UPDATE: I’ve revised this script and article since it’s original posting to better handle errors if you can’t test WSMAN. I also added support for alternate credentials, which is something you can’t do with Get-CimInstance.

NOTE: I should also point out that there is an apparent bug with Get-CimInstance. If you run the command for all instances of a given class, like win32_share and the remote computer is running PowerShell v2 you will get the error I’ve written about. But, if you use a query or a filter, it will work. So while this will fail if the computer is running v2, get-ciminstance win32_share -computername $c, these will work:

Get-CimInstance -query “Select * from win32_share” -computername $c
Get-CimInstance -win32_share -filter “Name=’C$'” -computername $c

It is an odd behavior, but it can work in your favor depending on how your structure your command. What I haven’t verified yet is what protocol PowerShell is using in these situations when the remote computer is running v2. Of course, the best, long term solution is to get all of your managed systems to at least PowerShell 3.0.

3 thoughts on “Get CIMInstance from PowerShell 2.0

  1. it´s a nice script and I am going to use it in my tools. I made one change in the function Test-IsWsman3 and want to share this: I noticed that when the WinRM Service is not configured or not responding the functions return not $true and not $false only the Error description from the Catch-block. Here is my suggestion of change:

    Try {
    $result = Test-WSMan -ComputerName $Computername -ErrorAction Stop
    Catch {
    Write-Error $_

    now if the WSMan is not responding at all, we get $false to the output stream and the Error message goes to the Error-Stream and not to the output stream

    • You bring up a good point that I missed. I modified Test-IsWSMAN3 to write the exception to the pipeline. Then in my Get-MyCimInstance function I can use Try/Catch to better handle the exception. If a computer is offline, I don’t want to get False because the function will keep going and eventually fail. So now, my test function is called. If it is True I go ahead and create the CIMSession. If it is False I create the CIMSessionOption and then the session. Or if there is an error I display a warning and move on to the next computer.

Comments are closed.