Get-ChildItem and Configuration Manager PowerShell Provider

This week I had the opportunity to speak at the Midwest Management Summit. This year I had two different sessions together with Ryan and Mike. With Ryan, we talked about automation capabilities in Configuration Manager.

Get-ChildItem is one of the cmdlets that you have used in PowerShell. On file system Get-ChildItem prints out files and folders, if you run the same cmdlet against the certificate store, then it prints out the certificates, etc. However, if you take the Get-Childitem cmdlet and you run it inside the Configuration Manager PowerShell provider, it only prints out the admin console folders but not the objects within that folder.

Get-ChildItem and Configuration Manager PowerShell Provider

Now you may have a question how can I get applications, collections or packages from a specific folder? If you are using folders like me, then sometimes you may need to change objects under a particular folder and not all the objects.

Luckily we have two different workarounds, and in both cases, we need to retrieve the objects directly through WMI using WMI cmdlets in PowerShell. So let’s take a closer look at how to do that.

Option 1

Here is the sample folder structure that we are going to work. In this case, we have one application under the Dell folder. In a production environment, of course, you may have multiple objects under one folder. However, for the demo purposes, we keep this simple.

So the first step for us is to identify the ContainerNodeID value. ContainerNodeID is the unique folder ID. To do that we need to use the following command:

This command returns the following output:

From this output, we need to copy out the ContainerNodeID property value, and we are going to use that in a WMI query.

Change the FolderID and SiteCode variable values and then execute. This code block should produce the following output:

As you see now, we have the Dell Command Monitor 2 application information in our PowerShell session. So let’s try to change the application name to something else using Configuration Manager PowerShell module cmdlets.

Set-CMApplication cmdlet failed because the object type is different. Object type must be Microsoft.ConfigurationManagement.ManagementProvider.IResultObject.

To fix this issue, we need to use ConvertTo-CMIResultObject cmdlet. This cmdlet allows us to convert WMI instance to Microsoft.ConfigurationManagement.ManagementProvider.IResultObject.

Yep, you heard it correctly. This cmdlet is awesome 🙂

So let’s run the following code block and see what happens

Now check the Dell folder in your Configuration Manager console.

Option 2

In this example, we are using the same folder structure, but the code will be a little bit different.

Let’s run the following code block:

In this example, I’m reading the data directly from SMS_ApplicationLatest WMI class. In that class, we have ObjectPath property, and that property holds the full path to object. We can do the same with Collections, Packages etc.

Have fun!