27 April 2017

Automated Import of PowerShell SCCM Module

While writing quite a few PowerShell scripts for SCCM, I got tired of having to look up the location of the SCCM PowerShell module to import. I decided while writing the current PowerShell SCCM script, I would automate this process from now on. This makes it a snap using SAPIEN's PowerShell Studio to make this function a snippet and quickly add it each time I write a new SCCM script.

This script allows you to import the SCCM module without having to look up the location. The only parameter you have to specify is the -SCCMServer, which is the name of the SCCM server. The script will then connect to the SCCM server, get the parent installation directory, including the drive letter, and then search for the ConfigurationManager.psd1 file. If it finds more than one file, it will import the latest one.

You can use the function in this script in other scripts to make the process a breeze.

The script can be downloaded from my GitHub repository.


 <#  
      .SYNOPSIS  
           Imports the SCCM PowerShell Module  
        
      .DESCRIPTION  
           This function will import the SCCM PowerShell module without the need of knowing the location. The only thing that needs to be specified is the name of the SCCM server.   
        
      .NOTES  
           ===========================================================================  
           Created with:     SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.139  
           Created on:       4/26/2017 3:56 PM  
           Created by:       Mick Pletcher  
           Filename:         ImportSCCMModule.ps1  
           ===========================================================================  
 #>  
 [CmdletBinding()]  
 param ()  
   
 function Import-SCCMModule {  
 <#  
      .SYNOPSIS  
           Import SCCM Module  
        
      .DESCRIPTION  
           Locate the ConfigurationManager.psd1 file and import it.  
        
      .PARAMETER SCCMServer  
           Name of the SCCM server to connect to.  
        
      .NOTES  
           Additional information about the function.  
 #>  
        
      [CmdletBinding()]  
      param  
      (  
           [ValidateNotNullOrEmpty()][string]$SCCMServer  
      )  
        
      #Get the architecture of the specified SCCM server  
      $Architecture = (get-wmiobject win32_operatingsystem -computername $SCCMServer).OSArchitecture  
      #Get list of installed applications  
      $Uninstall = Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ChildItem -Path REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -Force -ErrorAction SilentlyContinue }  
      If ($Architecture -eq "64-bit") {  
           $Uninstall += Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ChildItem -Path REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -Force -ErrorAction SilentlyContinue }  
      }  
      #Get the registry key that specifies the location of the SCCM installation drive and directory  
      $RegKey = ($Uninstall | Where-Object { $_ -like "*SMS Primary Site*" }) -replace 'HKEY_LOCAL_MACHINE', 'HKLM:'  
      $Reg = Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ItemProperty -Path $args[0] } -ArgumentList $RegKey  
      #Parse the directory listing  
      $Directory = (($Reg.UninstallString).Split("\", 4) | Select-Object -Index 0, 1, 2) -join "\"  
      #Locate the location of the SCCM module  
      $Module = Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ChildItem -Path $args[0] -Filter "ConfigurationManager.psd1" -Recurse } -ArgumentList $Directory  
      #If more than one module is present, use the latest one  
      If ($Module.Length -gt 1) {  
           foreach ($Item in $Module) {  
                If (($NewModule -eq $null) -or ($Item.CreationTime -gt $NewModule.CreationTime)) {  
                     $NewModule = $Item  
                }  
           }  
           $Module = $NewModule  
      }  
      #format the $Module unc path  
      [string]$Module = "\\" + $SCCMServer + "\" + ($Module.Fullname -replace ":", "$")  
      #Import the SCCM module  
      Import-Module -Name $Module  
 }  
   
 Import-SCCMModule -SCCMServer "SCCMServer"  
   

Related Posts:

  • Display All Logged On Users in a Domain If you are like me, sometimes you need to find a few machines where no users are logged in. I have written this script that will scan a list of machines and tell whether a user is logged on or not. It reads a list of machine… Read More
  • OpenLM Agent Silent Install Installing OpenLM Agent is a relatively straight-forward process. There are no settings required for the msi installer. In order to make it a silent install, you first need to install it on your own machine. Once installed,… Read More
  • Font Installation Script This script will install all fonts residing in the same folder as this script resides. I reads all of the font file names in the directory into an array. It then binds the script to the fonts folder and copies the fonts ove… Read More
  • Verify SCCM Base Build This will check for installed programs by verifying the existance of the uninstall registry key. This is located in the SCCM task sequencing just before the sysprep process. This also checks to see other settings on the mach… Read More
  • SCCM Advanced Client Cache Size Reporting This script will run a WMI query to gather the size of the advanced client cache and the amount of cache memory in use. It then makes a copy of the MIF file to be modified with the cache size and memory in use. It is then m… Read More

0 comments:

Post a Comment