A while back I wrote a PowerShell Module that showed some ways to do things in ConfigMgr using PowerShell. Since then I’ve added more and more things to my PowerShell library and thought it was time to demonstrate a slightly better way to import the ConfigMgr PowerShell module.
Anyone who has worked with the ConfigMgr PowerShell module has at some point experienced the moment when they attempt to run
import-module ConfigurationManager
only to receive a bunch of angry red text indicating that the specified module wasn’t found in any module directory.
This is caused by the ConfigMgr module not being loaded to the default PowerShell module library location when the console is installed but rather to the ConfigMgr install directory. So what can we do about that? Well fortunately we know that the ConfigMgr PowerShell cmdlets are all stored in the installation direction. We know this because if in the ConfigMgr console we hit the drop down and select ‘connect via Windows PowerShell ISE’ it will open a PowerShell Script in ISE that shows how the module is loaded and the PSDrive is changed.
This gives us the pieces we need to build some code since we now know the location we can build out the following code to ensure we have a full path to the location:
(Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)
When we run the above that will produce a path from wherever we currently are to the ConfigMgr directory where the module file lives. While this gives us a way to get the cmdlets I wanted to take things a step further so I wrote a function that allows me to import the module whenever I need it and attached it to my PowerShell profile. It looks something like this.
function Get-CMModule { [CmdletBinding()] param() Try { Write-Verbose "Attempting to import SCCM Module" Import-Module (Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) -Verbose:$false Write-Verbose "Successfully imported the SCCM Module" } Catch { Throw "Failure to import SCCM Cmdlets." } }
This now gets us to the point where we have the module loaded but of course we can’t do anything with the module until after we connect to the appropriate drive so I wrote two more functions. One is a generic function that tests if a module is loaded and then a second one that tests specifically for the ConfigMgr module and if you’re connected to a ConfigMgr PowerShell drive.
function Test-ConfigMgrAvailable { [CMdletbinding()] Param ( ) try { if((Test-Module -ModuleName ConfigurationManager) -eq $false){throw "You have not loaded the configuration manager module please load the appropriate module and try again."} write-Verbose "ConfigurationManager Module is loaded" Write-Verbose "Checking if current drive is a CMDrive" if((Get-location).Path -ne (Get-location -PSProvider 'CmSite').Path){throw "You are not currently connected to a CMSite Provider Please Connect and try again"} write-Verbose "Succesfully validated connection to a CMProvider" write-verbose "Passed all connection tests" return $true } catch { $errorMessage = $_.Exception.Message write-error -Exception CMPatching -Message $errorMessage return $false } } function Test-Module { [CMdletbinding()] Param ( [Parameter(Mandatory = $true)] [String]$ModuleName ) If(Get-Module -Name $ModuleName) { return $true } If((Get-Module -Name $ModuleName) -ne $true) { return $false } }
The above will allow you to confirm that the module is present and make sure that you are connected to the ConfigMgr PSDrive when running advanced scripts in your environment.
Hopefully this is helpful!
Will this work (with modifications if needed) from Win PE? I’m trying to build a pre-start utility to run in boot media that would allow desktop techs to delete the device from SCCM so they can reimage.
Unfortunately it won’t work in WinPE. You have to have installed the ConfigMgr console to have access to the required DLL files and the PSDrive information. However, if you take a look at some of the stuff that Nickolaj has done with the web front end and the webservice there is an option there that would allow you to do exactly what you’re talking about. Cheers!
Nice blog Jordan. Here are two more related links:
– https://blogs.catapultsystems.com/chsimmons/archive/2017/01/19/the-many-ways-to-import-the-configmgr-cmdlet-library-module
– https://gallery.technet.microsoft.com/scriptcenter/Make-Configuration-Manager-04474a87