Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
June 18, 2024

Native Win32_Directory Scripting

I hope you've had an opportunity to try the code examples from the last few articles. I'm not implying that using CIM classes is necessarily the the best way to get file and folder information, but it might be a good alternative in some situations. Or if you have a very specific use case, it might be the best way to get the information you need. If nothing else, you should be able to learn from the code techniques in my examples. There's nothing wrong with writing a function that uses `Get-CimInstance` and `Get-CimAssociatedInstance`. However, if you have the skills and information to take advantage of the native CIM classes, can eke out a little more performance. > *The one thing to keep in mind is that if need to write Pester tests for your code, you can't mock a CIM class method. You would need to write a wrapper or helper function to abstract the CIM class method. **That** you could mock in a Pester test.* To recap, code like this should work.

[CimSession]$cs = 'thinkx1-jh'

$query = "Select * from win32_directory where name = 'C:\\temp'"

$d = $cs.QueryInstances('Root/cimv2', 'WQL', $query)

$d | Get-CimAssociatedInstance -ResultClassName Win32_Directory |

Where-Object { $_.Name -match "^$($d.name.replace('\','\\'))" } |

Select-Object Name
This gives me all the folders in the root of `C:\Temp`.
Name

----

C:\temp\banana

C:\temp\contoso

C:\temp\docs

C:\temp\fonts

C:\temp\foo

C:\temp\ps
Let's assume we want to go native to get the associated instances. For that, we can use the `EnumerateAssociatedInstances` method.
PS C:\> Get-TypeMember Microsoft.Management.Infrastructure.CimSession -Name EnumerateAssociatedInstances | Tee -Variable m

   Type: Microsoft.Management.Infrastructure.CimSession

Name                         MemberType ResultType    IsStatic IsEnum

----                         ---------- ----------    -------- ------

EnumerateAssociatedInstances Method     IEnumerable`1

PS C:\> $m.Syntax

$obj.EnumerateAssociatedInstances([String]namespaceName,[CimInstance]sourceInstance,[String]associationClassName,[String]resultClassName,[String]sourceRole,[String]resultRole)

$obj.EnumerateAssociatedInstances([String]namespaceName,[CimInstance]sourceInstance,[String]associationClassName,[String]resultClassName,[String]sourceRole,[String]resultRole,[CimOperationOptions]options)
The method has a few overloads. The first one should meet our needs. Here's an easier way to view the parameters.
PS C:\> $m.syntax[0].split(",")

$obj.EnumerateAssociatedInstances([String]namespaceName

[CimInstance]sourceInstance

[String]associationClassName

[String]resultClassName

[String]sourceRole

[String]resultRole)
You may be thinking, "How do I know what values to use?" Fortunately, we can use the `Get-CimAssociatedInstance` cmdlet to get the information we need from Verbose output.
'Get-CimAssociatedInstance Verbose'
figure 1
Based on this, you would try something like this.
$cs.enumerateAssociatedInstances('root/cimv2', $d, '', 'Win32_Directory', '', '')
This will fail with an error of `MethodException: Cannot find an overload for "enumerateAssociatedInstances" and the argument count: "6".` That's not very helpful because the syntax calls for six parameters and that's what I did. Let me show you how to get around this problem and then we'll look at how to create some tooling around `CIM_DataFile` and `Win32_Directory`.
Get a premium subscription for full article and archive access

I hope you've had an opportunity to try the code examples from the last few articles. I'm not implying that using CIM classes is necessarily the the best way to get file and folder information, but it might be a good alternative in some situations. Or if you have a very specific use case, it might be the best way to get the information you need. If nothing else, you should be able to learn from the code techniques in my examples.

There's nothing wrong with writing a function that uses Get-CimInstance and Get-CimAssociatedInstance. However, if you have the skills and information to take advantage of the native CIM classes, can eke out a little more performance.

The one thing to keep in mind is that if need to write Pester tests for your code, you can't mock a CIM class method. You would need to write a wrapper or helper function to abstract the CIM class method. That you could mock in a Pester test.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.