22 November 2019

SCCM Client Installer for MDT

Recently, I wanted to revisit the process of installing the SCCM client during an MDT task sequence. At first, I tried to use the SCCM PowerShell module to initiate the install. I learned during testing that it does not work if a system is not present in SCCM. The process needed to include initializing the client deployment, waiting for the ccmsetup to begin, and then waiting until the ccmsetup disappears and ccmexec is running. An additional step was to run the install directly from the source folder on the SCCM server so the latest version will always get installed. I wanted SCCM to wait until the setup is complete before continuing because there is still the possibility that it could screw up if the task sequence proceeded to another task, and the system rebooted.

Once I finished rewriting this installer and verified it ran from the command line, I encountered the issue of the task failing. After several attempts, I realized it not only needed to be executed from a domain admin account, but the profile also needed to be loaded, as shown below. Once that was checked, the task completed without any errors. The reason this must be executed using a domain account is that in my environment, MDT is on a different server than SCCM, thereby not having access to it. One additional note is that I also shared out the folder where the client installer exists so that I can use \\<SCCM Server>\SCCMInstaller as the path to the ccmsetup.exe file. 



In the script below, it first initializes the ccmsetup.exe. It then waits for ccmsetup.exe to appear as a running task. It waits for ccmsetup.exe to disappear, and ccmexec.exe appears in the task list for the script to end with an error code 0. I included error codes in this so that I will know if something happens that causes the script to begin failing in order to mitigate the problem. 

The script below can be downloaded from my GitHub site.


 <#  
      .SYNOPSIS  
           Install SCCM Client  
        
      .DESCRIPTION  
           Install the SCCM client from the client installation folder located on the SCCM server.  
        
      .PARAMETER MP  
           Management Point  
        
      .PARAMETER FSP  
           Fallback Status Point  
        
      .PARAMETER SiteCode  
           Three letter SiteCode  
        
      .PARAMETER ClientPath  
           Network location on the SCCM server where the client resides  
        
      .NOTES  
           ===========================================================================  
           Created with:     SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.142  
           Created on:       11/21/2019 4:14 PM  
           Created by:       Mick Pletcher  
           Filename:         SCCMClientInstaller.ps1  
           ===========================================================================  
 #>  
   
 [CmdletBinding()]  
 param  
 (  
      [ValidateNotNullOrEmpty()]  
      [string]$MP,  
      [ValidateNotNullOrEmpty()]  
      [string]$FSP,  
      [ValidateNotNullOrEmpty()]  
      [string]$SiteCode,  
      [ValidateNotNullOrEmpty()]  
      [string]$ClientPath  
 )  
   
 #Add backslash to end of $ClientPath if it does not exist  
 If ($ClientPath.Substring($ClientPath.Length - 1) -ne '\') {  
      $ClientPath += '\'  
 }  
 Write-Host 'Initiating SCCM Client Installation.....'  
 #Execute the ccmsetup.exe file  
 $ExitCode=(Start-Process -FilePath ($ClientPath + 'ccmsetup.exe') -ArgumentList ('/mp:' + $MP + [char]32 + 'SMSSITECODE=' + $SiteCode + [char]32 + 'FSP=' + $FSP) -PassThru -WindowStyle Minimized -Wait).ExitCode  
 If ($ExitCode -eq 0) {  
      Write-Host 'Waiting for installation to begin.....'  
      $StartTime = Get-Date  
      #Wait until the ccmsetup.exe appears in the task manager  
   Do {  
     $Process = (Get-Process -Name ccm*).Name  
           $TimeSpan = New-TimeSpan -Start $StartTime -End (Get-Date)  
           #Exit if it takes more than 300 seconds for the ccmsetup.exe to begin  
     If ($TimeSpan.TotalSeconds -gt 300) {  
       Exit 2  
     }  
   } While ($Process -notcontains 'ccmsetup')  
      Write-Host 'Installing SCCM Client.....' -NoNewline  
      $StartTime = Get-Date  
      #Wait until ccmsetup.exe closes  
   Do {  
     $Process = (Get-Process -Name ccm*).Name  
     $TimeSpan = New-TimeSpan -Start $StartTime -End (Get-Date)  
           #Exit with error code 3 if the ccmsetup.exe runs longer than 600 seconds  
           If ($TimeSpan.TotalSeconds -gt 600) {  
       Exit 3  
     }  
   } While ($Process -contains 'ccmsetup')  
 } else {  
   Exit 1  
 }  
 #Exit with error code 0 if ccmexec.exe is running in the task manager, otherwise exit with an error code 4  
 If ((Get-Process -Name CcmExec) -ne $null) {  
      Write-Host 'SCCM Client Successfully installed' -ForegroundColor Yellow  
 }  
 else {  
      Write-Host 'SCCM Client installation failed' -ForegroundColor Red  
      Exit 4  
 }  
   

0 comments:

Post a Comment