Recently, I had the privilege of speaking at the Midwest Management Summit, where I, along with Ryan and Mike, delved into the automation capabilities of Configuration Manager. One of the key topics we discussed was the Get-ChildItem cmdlet in PowerShell, a tool that many of you have likely used. However, its behavior changes when used within the Configuration Manager PowerShell Provider, and today, we’re going to explore that.
Importing the Configuration Manager PowerShell Module
Before we begin working with the Configuration Manager PowerShell Provider, it’s essential to import the Configuration Manager module. This module contains the necessary cmdlets and functions for interacting with Configuration Manager using PowerShell. To import the module, follow these steps:
- Open PowerShell and ensure you have administrator privileges.
- Run the following commands to import the Configuration Manager PowerShell module:
# Import Module
$SiteCode = Get-PSDrive -PSProvider CMSITE
These commands import the Configuration Manager module and set the location to the appropriate drive for your Configuration Manager site.
In a standard PowerShell environment Get-ChildItem is a cmdlet that can list files and folders on a file system, or even print out certificates when run against a certificate store. However, when you run the Get-ChildItem cmdlet inside the Configuration Manager PowerShell provider, it only prints out the admin console folders, not the objects within that folder. This might lead you to ask, “How can I get applications, collections, or packages from a specific folder?“
If you run the Get-Childitem cmdlet inside the Configuration Manager PowerShell provider. In that case, it only prints out the admin console folders but not the objects within that folder.
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, 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.
Here is the sample folder structure.
In this case, we have two applications under the Defender folder. In a production environment, you may have multiple objects under one folder. However, for 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:
Get-ChildItem PS1:\Package\OSD | Select-Object -Property Name,ContainerNodeID,ObjectType
This command returns an output from which we need to copy out the ContainerNodeID property value. We then use this value in a WMI query.
From this output, we need to copy out the ContainerNodeID property value, and we will use that in a WMI query.
Change the FolderID and SiteCode variable values and then execute. This code block should produce the following output:
$FolderID = 16777233 # Use the WMI tool to get the FolderID
$CollectionsInSpecficFolder = Get-WmiObject -Namespace "ROOT\SMS\Site_PS1" `
-Query "select * from SMS_Package where PackageID is in(select InstanceKey from SMS_ObjectContainerItem where ObjectType='2' and ContainerNodeID='$FolderID')"
In summary, Get-ChildItem behaves differently in the Configuration Manager PowerShell Provider. But with the power of WMI, we can retrieve all the information we need and work around this shortcoming.