Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
June 6, 2024

More CimSession Toolmaking

In the last article, I started demonstrating how you can build your own PowerShell tooling using the CIM .NET classes. While I still recommend using cmdlets like `Get-CimInstance` and `Get-CimClass` in your functions, advanced scripters may want to build tooling that meets a specific requirement the CIM cmdlets don't address. Today, let's continue to see how we can use the .NET CIM classes to build a custom function that retrieves the CIM-based information. I'll begin with creating a `CimSession` object for the local computer.

$cs =[Microsoft.Management.Infrastructure.CimSession]::Create($env:COMPUTERNAME)
I'm going to focus on getting and enumerating CIM classes and instances.
PS C:\> $cs | Get-Member -MemberType method |

Where Name -match '^((Get)|(Query)|(Enumerate))((Class(es)?)|(Instance(s)?))$' |

Select-Object Name

Name

----

EnumerateClasses

EnumerateInstances

GetClass

GetInstance

QueryInstances
Once you can write code with these methods, then you can branch out into more advanced CIM topics like `Associations`.
Get a premium subscription for full article and archive access

In the last article, I started demonstrating how you can build your own PowerShell tooling using the CIM .NET classes. While I still recommend using cmdlets like Get-CimInstance and Get-CimClass in your functions, advanced scripters may want to build tooling that meets a specific requirement the CIM cmdlets don't address. Today, let's continue to see how we can use the .NET CIM classes to build a custom function that retrieves the CIM-based information.

I'll begin with creating a CimSession object for the local computer.

$cs =[Microsoft.Management.Infrastructure.CimSession]::Create($env:COMPUTERNAME)
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.