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