03 October 2019

Importing and Using the SCCM PowerShell Module

Recently, I have begun setting up new front and back-office security runbooks in Microsoft Orchestrator. These runbooks needed to use PowerShell for getting data from the SCCM server. The SCCM console is not installed on the Orchestrator server, so PowerShell required to be able to connect directly to it.

The following script will import the SCCM PowerShell module, connect to the SCCM server, and then disconnect. I used the cmdlet Get-CMCollectionMember for all systems because that is shared in all SCCMs.

The first thing that needs to be done is to define the UNC path to the SCCM module. The module is located at Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1. I would put the drive letter in front, but like myself, you may not have SCCM installed on the c: drive. The next thing that needs to be defined is the SCCM server name. I used the FQDN for that. The SCCM Site Description is needed to map the drive. The description was made up of calling it Primary SCCM Site. Finally, the three-letter site code is necessary. That is the directory you will need to change to inside PowerShell before you can call any cmdlets.

Once these items are defined, you will need to first import the SCCM module. Next, you will need to open the connection to it through using the New-PSDrive. The directory then needs to be changed to the defined PSDrive. Now the SCCM cmdlets can be utilized. I included removing the PSDrive at the end of the script.

You can download the script from my GitHub site.

           PowerShell SCCM Connection  
           This script will connect to the SCCM server and return a list of all systems in SCCM. It is a demo on how to accomplish this task.  
      .PARAMETER SCCMModule  
           UNC path including file name of the configuration manager module  
      .PARAMETER SCCMServer  
           A description of the SCCMServer parameter.  
      .PARAMETER SCCMSiteDescription  
           Description of the SCCM Server  
      .PARAMETER SiteCode  
           Three letter SCCM Site Code  
           Fully Qualified Domain Name of the SCCM server  
           Created with:     SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.142  
           Created on:       4/24/2019 9:56 AM  
           Created by:       Mick Pletcher  
           Filename:         SCCMCleanup.ps1  
      [string]$SCCMModule = '<UNCPath>\ConfigurationManager.psd1',  
      [string]$SCCMServer = '<FQDN of SCCM Server>',  
      [string]$SCCMSiteDescription = '<SCCM Server Description>',  
      [string]$SiteCode = '<Three letter SCCM Site Code>'  
 Import-Module -Name $SCCMModule -Force  
 New-PSDrive -Name $SiteCode -PSProvider 'AdminUI.PS.Provider\CMSite' -Root $SCCMServer -Description $SCCMSiteDescription | Out-Null  
 Set-Location -Path ($SiteCode + ':')  
 $List = Get-CMCollectionMember -CollectionName 'All Systems'  
 Remove-PSDrive -Name $SiteCode -Force  
 Write-Output $List.Name  



Post a Comment