07 August 2015

SCCM Client Installer

This PowerShell script will uninstall previous versions of SCCM client, run the CCMClean to make sure all instances of the client are gone off of the machine, and finally install the SCCM client. The script will not complete until the installation of the SCCM client is completed. I used this when upgrading from 2007 to 2012 and also have this integrated into the build. I pulled the functions from my installation module and put them into this script so that it can be a standalone. One thing you will need to do, if you want to include the ccmclean.exe, is to download it from here.

You can download the script below from here.


1:  <#       
2:       .NOTES  
3:       ===========================================================================  
4:        Created with:      SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.90  
5:        Created on:       7 August 2015 12:18 PM  
6:        Created by:       Mick Pletcher  
7:        Organization:        
8:        Filename:        installSCCMClient_Standalone.ps1  
9:       ===========================================================================  
10:       .DESCRIPTION  
11:            This script will uninstall all previous versions of SCCM Client,  
12:            including the execution of the ccmclean.exe to rid the system of  
13:            any remnants of the previous client. It will then install the new  
14:            client and wait until the client is installed and is communicating  
15:            with the SCCM server.  
16:    
17:  #>  
18:    
19:  #Declare Global Variables  
20:  Set-Variable -Name RelativePath -Scope Global -Force  
21:  Set-Variable -Name Title -Scope Global -Force  
22:    
23:  Function InitializeVariables {  
24:       $Global:RelativePath = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent) + "\"  
25:       $Global:Title = "SCCM 2012 R2 Client"  
26:  }  
27:    
28:  Function Install-EXE {  
29:       <#  
30:       .SYNOPSIS  
31:            Install-EXE  
32:       .DESCRIPTION  
33:            Installs an EXE file  
34:       .EXAMPLE  
35:            Install-EXE -DisplayName "Microsoft Office 2013" -Executable "c:\temp\install.exe" -Switches "/passive /norestart"  
36:       #>  
37:         
38:       Param ([String]$DisplayName,  
39:            [String]$Executable,  
40:            [String]$Switches)  
41:         
42:       Write-Host "Install"$DisplayName"....." -NoNewline  
43:       If ((Test-Path $Executable) -eq $true) {  
44:            $ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Switches -Wait -Passthru).ExitCode  
45:       } else {  
46:            $ErrCode = 1  
47:       }  
48:       If (($ErrCode -eq 0) -or ($ErrCode -eq 3010)) {  
49:            Write-Host "Success" -ForegroundColor Yellow  
50:       } else {  
51:            Write-Host "Failed with error code "$ErrCode -ForegroundColor Red  
52:       }  
53:  }  
54:    
55:  Function Install-MSP {  
56:       <#  
57:       .SYNOPSIS  
58:            Install-MSP  
59:       .DESCRIPTION  
60:            Installs an MSP patch  
61:       .EXAMPLE  
62:            Install-MSP -DisplayName "KB977203" -MSP $Global:RelativePath"i386\sccm2007ac-sp2-kb977203-x86.msp" -Switches "/qb- /norestart"  
63:       #>  
64:         
65:       Param ([String]$DisplayName,  
66:            [String]$MSP,  
67:            [String]$Switches)  
68:         
69:       $Executable = $Env:windir + "\system32\msiexec.exe"  
70:       $Parameters = "/p " + [char]34 + $MSP + [char]34 + [char]32 + $Switches  
71:       Write-Host "Install"$DisplayName"....." -NoNewline  
72:       $ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Parameters -Wait -Passthru).ExitCode  
73:       If (($ErrCode -eq 0) -or ($ErrCode -eq 3010)) {  
74:            Write-Host "Success" -ForegroundColor Yellow  
75:       } else {  
76:            Write-Host "Failed with error code "$ErrCode -ForegroundColor Red  
77:       }  
78:  }  
79:    
80:  Function Set-ConsoleTitle {  
81:       <#  
82:       .SYNOPSIS  
83:            Set-ConsoleTitle  
84:       .DESCRIPTION  
85:            Renames the PowerShell console window  
86:       .EXAMPLE  
87:            Set-ConsoleTitle -Title "Test"  
88:       #>  
89:         
90:       Param ([String]$Title)  
91:       $host.ui.RawUI.WindowTitle = $Title  
92:  }  
93:    
94:  Function Stop-Task {  
95:       <#  
96:       .SYNOPSIS  
97:            Stop-Task  
98:       .DESCRIPTION  
99:            Kills a designated Task  
100:       .EXAMPLE  
101:            Stop-Task -Process "outlook"  
102:       #>  
103:         
104:       Param ([String]$Process)  
105:         
106:       $Proc = Get-Process $Process -ErrorAction SilentlyContinue  
107:       Write-Host "Killing"$Process"....." -NoNewline  
108:       If ($Proc -ne $null) {  
109:            Do {  
110:                 $ProcName = $Process + ".exe"  
111:                 $Temp = taskkill /F /IM $ProcName  
112:                 Start-Sleep -Seconds 2  
113:                 $Proc = $null  
114:                 $Proc = Get-Process $Process -ErrorAction SilentlyContinue  
115:            } While ($Proc -ne $null)  
116:            Write-Host "Closed" -ForegroundColor Yellow  
117:       } else {  
118:            Write-Host "Already Closed" -ForegroundColor Green  
119:       }  
120:  }  
121:    
122:  Function Uninstall-EXE {  
123:       <#  
124:       .SYNOPSIS  
125:            Uninstall-EXE  
126:       .DESCRIPTION  
127:            Uninstalls an EXE file  
128:       .EXAMPLE  
129:            Uninstall-EXE -DisplayName "Microsoft Office 2013" -Executable "c:\temp\setup.exe" -Switches "/uninstall /passive /norestart"  
130:       #>  
131:         
132:       Param ([String]$DisplayName,  
133:            [String]$Executable,  
134:            [String]$Switches)  
135:         
136:       Write-Host "Uninstall"$DisplayName"....." -NoNewline  
137:       $ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Switches -Wait -Passthru).ExitCode  
138:       If (($ErrCode -eq 0) -or ($ErrCode -eq 3010)) {  
139:            Write-Host "Success" -ForegroundColor Yellow  
140:       } else {  
141:            Write-Host "Failed with error code "$ErrCode -ForegroundColor Red  
142:       }  
143:  }  
144:    
145:  Function Uninstall-MSIByGUID {  
146:       <#  
147:       .SYNOPSIS  
148:            Uninstall-MSIByGUID  
149:       .DESCRIPTION  
150:            Uninstalls an MSI application using the GUID  
151:       .EXAMPLE  
152:            Uninstall-MSIByGUID -DisplayName "Workshare Professional" -GUID "{8686EC18-6282-4AA9-92AC-2865B972E244}" -Switches "/qb- /norestart"  
153:       #>  
154:         
155:       Param ([String]$DisplayName,  
156:            [String]$GUID,  
157:            [String]$Switches)  
158:         
159:       $Executable = $Env:windir + "\system32\msiexec.exe"  
160:       $Parameters = "/x " + $GUID + [char]32 + $Switches  
161:       Write-Host "Uninstall"$DisplayName"....." -NoNewline  
162:       $ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Parameters -Wait -Passthru).ExitCode  
163:       If (($ErrCode -eq 0) -or ($ErrCode -eq 3010)) {  
164:            Write-Host "Success" -ForegroundColor Yellow  
165:       } elseIf ($ErrCode -eq 1605) {  
166:            Write-Host "Not Present" -ForegroundColor Green  
167:       } else {  
168:            Write-Host "Failed with error code "$ErrCode -ForegroundColor Red  
169:       }  
170:  }  
171:    
172:  Function Uninstall-MSIByName {  
173:       <#  
174:       .SYNOPSIS  
175:            Uninstall-MSIByName  
176:       .DESCRIPTION  
177:            Uninstalls an MSI application using the MSI file  
178:       .EXAMPLE  
179:            Uninstall-MSIByName -ApplicationName "Adobe Reader" -Switches "/qb- /norestart"  
180:       #>  
181:         
182:       Param ([String]$ApplicationName,  
183:            [String]$Switches)  
184:         
185:       $Uninstall = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Recurse -ea SilentlyContinue  
186:       $Uninstall += Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall -Recurse -ea SilentlyContinue  
187:       $SearchName = "*" + $ApplicationName + "*"  
188:       $Executable = $Env:windir + "\system32\msiexec.exe"  
189:       Foreach ($Key in $Uninstall) {  
190:            $TempKey = $Key.Name -split "\\"  
191:            If ($TempKey[002] -eq "Microsoft") {  
192:                 $Key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + $Key.PSChildName  
193:            } else {  
194:                 $Key = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" + $Key.PSChildName  
195:            }  
196:            If ((Test-Path $Key) -eq $true) {  
197:                 $KeyName = Get-ItemProperty -Path $Key  
198:                 If ($KeyName.DisplayName -like $SearchName) {  
199:                      $TempKey = $KeyName.UninstallString -split " "  
200:                      If ($TempKey[0] -eq "MsiExec.exe") {  
201:                           Write-Host "Uninstall"$KeyName.DisplayName"....." -NoNewline  
202:                           $Parameters = "/x " + $KeyName.PSChildName + [char]32 + $Switches  
203:                           $ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Parameters -Wait -Passthru).ExitCode  
204:                           If (($ErrCode -eq 0) -or ($ErrCode -eq 3010) -or ($ErrCode -eq 1605)) {  
205:                                Write-Host "Success" -ForegroundColor Yellow  
206:                           } else {  
207:                                Write-Host "Failed with error code "$ErrCode -ForegroundColor Red  
208:                           }  
209:                      }  
210:                 }  
211:            }  
212:       }  
213:  }  
214:    
215:  Function Wait-ProcessEnd {  
216:       <#  
217:       .SYNOPSIS  
218:            Wait-Process  
219:       .DESCRIPTION  
220:            Waits for a Process to end before continuing.  
221:       .EXAMPLE  
222:            Wait-Process -Process "explorer"  
223:       #>  
224:         
225:       Param ([String]$Process)  
226:         
227:       Write-Host "Waiting for"$Process" to end....." -NoNewline  
228:       $Proc = Get-Process $Process -ErrorAction SilentlyContinue  
229:       If ($Proc -ne $null) {  
230:            Do {  
231:                 Start-Sleep -Seconds 5  
232:                 $Proc = Get-Process $Process -ErrorAction SilentlyContinue  
233:            } While ($Proc -ne $null)  
234:            Write-Host "Ended" -ForegroundColor Yellow  
235:       } else {  
236:            Write-Host "Process Already Ended" -ForegroundColor Yellow  
237:       }  
238:  }  
239:    
240:  cls  
241:  InitializeVariables  
242:  Set-ConsoleTitle -Title $Global:Title  
243:  Stop-Task -Process "msiexec"  
244:  Uninstall-MSIByName -ApplicationName "Configuration Manager Client" -Switches "/qb- /norestart"  
245:  Stop-Task -Process "msiexec"  
246:  Uninstall-MSIByGUID -DisplayName "SCCM 2007 Client" -GUID "{2609EDF1-34C4-4B03-B634-55F3B3BC4931}" -Switches "/qb- /norestart"  
247:  Stop-Task -Process "msiexec"  
248:  Uninstall-MSIByGUID -DisplayName "SCCM 2012 Client" -GUID "{BFDADC41-FDCD-4B9C-B446-8A818D01BEA3}" -Switches "/qb- /norestart"  
249:  Stop-Task -Process "msiexec"  
250:  Uninstall-EXE -DisplayName "CCMClean" -Executable $Global:RelativePath"ccmclean.exe" -Switches "/all /logdir:%windir%\waller\logs /removehistory /q"  
251:  Stop-Task -Process "msiexec"  
252:  Install-EXE -DisplayName "SCCM 2012 R2 Client" -Executable $Global:RelativePath"ccmsetup.exe" -Switches "/mp:bnasccm.wallerlaw.int SMSSITECODE=BNA"  
253:  Wait-ProcessEnd -Process "CCMSETUP"  
254:  Stop-Task -Process "msiexec"  
255:    
256:  #Cleanup Global Variables  
257:  Remove-Variable -Name RelativePath -Scope Global -Force  
258:  Remove-Variable -Name Title -Scope Global -Force  

0 comments:

Post a Comment