-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-CMScriptContent.ps1
More file actions
339 lines (302 loc) · 14.3 KB
/
Update-CMScriptContent.ps1
File metadata and controls
339 lines (302 loc) · 14.3 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
[CmdletBinding(DefaultParameterSetName = "SearchOnly")]
param (
# UNC root for search
[Parameter(Mandatory = $true)]
[string]
$SearchBase,
# String to find in FileTypes
[Parameter(Mandatory = $true)]
[string[]]
$SearchStrings,
# String to replace found strings
[Parameter(Mandatory = $false, ParameterSetName = "Replace")]
[string]
$ReplaceString,
# File extensions to include in search
[Parameter()]
[string[]]
$Include = @("bat", "ps1", "sh", "cmd", "txt"),
# Do not confirm replace
[Parameter(ParameterSetName = "Replace")]
[switch]
$Force,
# SiteServer to get Applications
[Parameter(Mandatory = $false, ParameterSetName = "SearchOnly")]
[Parameter(Mandatory = $true, ParameterSetName = "Replace")]
[string]
$SiteServer,
# SiteCode to get Applications
[Parameter(Mandatory = $false, ParameterSetName = "SearchOnly")]
[Parameter(Mandatory = $true, ParameterSetName = "Replace")]
[string]
$SiteCode,
# Path to save transcript
[Parameter()]
[string]
$TranscriptPath = "./UpdateCMScriptContent.log",
# Extension for the backup files
[Parameter(ParameterSetName = "Replace")]
[string]
$BackupExtension = "sr_bkp"
)
BEGIN {
############################### Setup Search ###############################
$BackupExtension = $BackupExtension -replace "^\.?", "."
$Include = $Include | ForEach-Object { $_ -replace "^\.?", "." }
$SearchExp = "(" + (($SearchStrings | ForEach-Object { [regex]::Escape($_) }) -join "|") + ")"
function Get-IsPathPart {
param (
[Parameter(Position=0)]
[string]$FilePath,
[Parameter(Position=1)]
[string]$PathPart
)
$FilePathDomain = $FilePath | Select-String -Pattern "(?<=\\\\.*?)\..*?(?=\\.*)" |
Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value
$PathPartDomain = $PathPart | Select-String -Pattern "(?<=\\\\.*?)\..*?(?=\\.*)" |
Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value
if ($FilePathDomain -and $PathPartDomain -and ($FilePathDomain -ne $PathPartDomain) ) {
return $false
}
$FilePath = $FilePath -isplit "(?<=\\\\.*?)\..*?(?=\\.*)" -join ""
$PathPart = $PathPart -isplit "(?<=\\\\.*?)\..*?(?=\\.*)" -join ""
return $FilePath -imatch [regex]::Escape($PathPart)
}
function Update-FileContent {
param (
[Parameter(Mandatory)]
[System.IO.FileInfo]
$File,
[Parameter(Mandatory)]
[string]
$SearchExp,
[Parameter(Mandatory)]
[string]
$ReplaceString
)
try {
Copy-Item -Path $File.PSPath -Destination "$($FILE.PSPath)$BackupExtension" -ErrorAction "Stop" | Out-Null
} catch {
Write-Host "ERROR: Failed to backup '$($File.FullName)'. Skipping." -ForegroundColor "Red"
return $false
}
try {
(Get-Content -Path $File.PSPath -Raw -ErrorAction "Stop") -ireplace "$SearchExp", "$ReplaceString" |
Set-Content -Path $File.PSPath -ErrorAction "Stop"
Write-Host "INFO: Successfuly updated '$($File.FullName)'" -ForegroundColor "Green"
}
catch {
Write-Host "ERROR: Failed to write content for '$($File.FullName)'. Skipping." -ForegroundColor "Red"
return $false
}
return $true
}
Start-Transcript -Path $TranscriptPath
########################### Import dependancies ############################
try {
Import-Module "$PSScriptRoot\Get-CMContentPaths\Get-CMContentPaths.psm1" -ErrorAction Stop
} catch {
Write-Host "ERROR: Failed to import dependancies. Exiting." -ForegroundColor "Red"
Stop-Transcript
exit 1
}
############################## Verify Replace ##############################
if (-Not $Force -And $ReplaceString) {
Write-Host @"
You have included a `$ReplaceString. This will replace all instances of the search strings with the replace string.
Search String(s): '$($SearchStrings -join "' and '")'
Replace String: '$ReplaceString'.
"@ -ForegroundColor "Yellow"
$response = Read-Host -Prompt "Are you sure you would like to continue (yes/no)?"
if ($response.ToLower() -ne "yes") {
Write-Host "INFO: Stopping"
Stop-Transcript
exit 0
}
}
####################### Mount Endpoint Manager site ########################
if ($ReplaceString) {
Write-Host "INFO: Mounting Endpoint Manager site"
$OriginalLocation = Get-Location
if($null -eq (Get-Module ConfigurationManager)) {
try {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" -Global
} catch {
Write-Host "ERROR: Unable to import the ConfigurationManager.psd1 Cmdlets. Exiting." -ForegroundColor "Red"
exit 1
}
}
if($null -eq (Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue)) {
try {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $SiteServer -ErrorAction "Stop" | Out-Null
} catch {
Write-Host "ERROR: Unable to mount the Endpoint Manager site. Exiting." -ForegroundColor "Red"
exit 1
}
}
}
}
PROCESS {
################## Get files with extentions in $Include ##################
$Activity = "Searching for included file types at '$SearchBase'"
Write-Progress -Activity $Activity -Status "This may take a while..."
Write-Host "INFO: Starting search for files."
$FilesToScan = Get-ChildItem -File -Recurse -Path $SearchBase -ErrorAction SilentlyContinue |
Where-Object { $_.Extension -in $Include } | Sort-Object -Property "FullName"
$FilesToScanCount = $FilesToScan.Count
Write-Host "INFO: Got $FilesToScanCount files from '$SearchBase'"
if (-Not $FilesToScan) {
Write-Host "WARN: No files were found with included extentions. Exiting." -ForegroundColor "Yellow"
Stop-Transcript
exit 0
}
################### Search each file for search strings ###################
Write-Host "INFO: Starting file scan for '$($SearchStrings -join "' and '")'"
$Activity = "Scanning $FilesToScanCount files for '$($SearchStrings -join "' and '")'"
$Count = 0
$FilesWithString = New-Object -TypeName System.Collections.ArrayList
foreach ($File in $FilesToScan) {
Write-Progress -Activity $Activity -CurrentOperation "$($File.FullName)" -PercentComplete ((++$Count / $FilesToScanCount) * 100)
try {
if ((Get-Content -Path $File.PSPath -Raw -ErrorAction Stop) -imatch "$SearchExp") {
$FilesWithString.Add($File) | Out-Null
Write-Host "INFO: Found '$($File.FullName)'" -ForegroundColor "Green"
}
}
catch {
Write-Host "WARN: Unable to read content of '$($File.FullName)'. Skipping." -ForegroundColor "Yellow"
}
}
$FilesWithStringCount = $FilesWithString.Count
Write-Host "INFO: Found '$($SearchStrings -join "' or '")' in $FilesWithStringCount files"
if (-Not $FilesWithStringCount) {
Write-Host "INFO: No files were found content that includes the search strings. Exiting"
Stop-Transcript
exit 0
}
########### Replace search string in content with replace string ###########
if ($ReplaceString) {
$Activity = "Writing changes to files"
$Status = "'$($SearchStrings -join "' and '")' -> '$ReplaceString'"
$Count = 0
$FilesWithStringReplaced = New-Object -TypeName System.Collections.ArrayList
try {
Write-Host "INFO: Getting application with content from Endpoint Manager"
$CMAppList = Get-CMApplicationContentPaths -SiteServer $SiteServer -SiteCode $SiteCode
Write-Host "INFO: Got $($CMAppList.Count) applications with content"
} catch {
Write-Host "WARN: $($CMAppList.Count) applications with content" -ForegroundColor "Yellow"
}
try {
Write-Host "INFO: Getting packages with content from Endpoint Manager"
$CMPkgList = Get-CMPackageContentPaths -SiteServer $SiteServer -SiteCode $SiteCode
Write-Host "INFO: Got $($CMPkgList.Count) packages with content"
} catch {
Write-Host "WARN: $($CMPkgList.Count) packages with content" -ForegroundColor "Yellow"
}
Write-Host "INFO: Starting string replacement with '$ReplaceString'"
######## Match found files with MECM application content paths ########
Write-Host "INFO: Starting replacement for application affiliated files"
$ErrorCount = 0
foreach ($App in $CMAppList) {
foreach ($DepType in $App.DeploymentTypes){
$MatchedFiles = @()
$NeedsRedist = $false
foreach ($ContentPath in $DepType.ContentPaths) {
$MatchedFiles += $FilesWithString | Where-Object { Get-IsPathPart -FilePath $_.FullName -PathPart $ContentPath }
}
if ($MatchedFiles) {
$NeedsRedist = $true
foreach ($File in $MatchedFiles) {
Write-Progress -Activity $Activity -Status $Status -CurrentOperation "$($App.Name)" -PercentComplete ((++$Count / $FilesWithStringCount) * 100)
$Success = Update-FileContent -File $File -SearchExp $SearchExp -ReplaceString $ReplaceString
if ($Success) {
$FilesWithString.Remove($File) | Out-Null
$FilesWithStringReplaced.Add($File) | Out-Null
} else {
$ErrorCount++
}
}
} else {
foreach ($ContentPath in $DepType.ContentPaths) {
if ($FilesWithStringReplaced | Where-Object { Get-IsPathPart -FilePath $_.FullName -PathPart $ContentPath }) {
$NeedsRedist = $true
}
}
}
if ($NeedsRedist) {
Write-Host "INFO: '$($App.Name):$($DepType.Name)' needs distribution point update"
try {
Set-Location "$($SiteCode):\"
Update-CMDistributionPoint -ApplicationName "$($App.Name)" -DeploymentTypeName "$($DepType.Name)" -ErrorAction "Stop"
Write-Host "INFO: Successfully started distribution point update for '$($App.Name):$($DepType.Name)'"
} catch {
$ErrorCount++
Write-Host "ERROR: Failed to start distribution point update for '$($App.Name):$($DepType.Name)'" -ForegroundColor "Red"
} finally {
Set-Location $OriginalLocation.Path
}
}
}
}
if ($ErrorCount) {
Write-Host "WARN: Application affiliated file updates completed with $ErrorCount errors." -ForegroundColor "Yellow"
}
########## Match found files with MECM package content paths ##########
Write-Host "INFO: Starting replacement for package affiliated files"
$ErrorCount = 0
foreach ($Pkg in $CMPkgList) {
$NeedsRedist = $false
$MatchedFiles = $FilesWithString | Where-Object { Get-IsPathPart -FilePath $_.FullName -PathPart $Pkg.ContentPath }
if ($MatchedFiles) {
$NeedsRedist = $true
foreach ($File in $MatchedFiles) {
Write-Progress -Activity $Activity -Status $Status -CurrentOperation "$($Pkg.Name)" -PercentComplete ((++$Count / $FilesWithStringCount) * 100)
$Success = Update-FileContent -File $File -SearchExp $SearchExp -ReplaceString $ReplaceString
if ($Success) {
$FilesWithString.Remove($File) | Out-Null
$FilesWithStringReplaced.Add($File) | Out-Null
} else {
$ErrorCount++
}
}
} else {
if ($FilesWithStringReplaced | Where-Object { Get-IsPathPart -FilePath $_.FullName -PathPart $Pkg.ContentPath }) {
$NeedsRedist = $true
}
}
if ($NeedsRedist) {
Write-Host "INFO: '$($Pkg.Name)' needs distribution point update"
try{
Set-Location "$($SiteCode):\"
Update-CMDistributionPoint -PackageName "$($Pkg.Name)" -ErrorAction "Stop"
Write-Host "INFO: Successfully started distribution point update for '$($Pkg.Name)'" -ForegroundColor Green
} catch {
$ErrorCount++
Write-Host "ERROR: Failed to start distribution point update for $($Pkg.Name)" -ForegroundColor "Red"
} finally {
Set-Location $OriginalLocation.Path
}
}
}
if ($ErrorCount) {
Write-Host "WARN: Package affiliated file updates completed with $ErrorCount errors." -ForegroundColor "Yellow"
}
################### Replace remaining files content ###################
Write-Host "INFO: Starting replacement for non-affiliated files"
$ErrorCount = 0
foreach ($File in $FilesWithString) {
Write-Progress -Activity $Activity -Status $Status -CurrentOperation "$($File.FullName)" -PercentComplete ((++$Count / $FilesWithStringCount) * 100)
$Success = Update-FileContent -File $File -SearchExp $SearchExp -ReplaceString $ReplaceString
if (-Not $Success) { $ErrorCount++ }
}
if ($ErrorCount) {
Write-Host "WARN: Non-affiated file updates completed with $ErrorCount errors." -ForegroundColor "Yellow"
}
}
}
END {
Write-Host "INFO: Operation Complete"
Write-Progress -Activity "Operation Complete" -Completed
Stop-Transcript
}