-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathSCCMActions.ps1
More file actions
455 lines (425 loc) · 16.8 KB
/
SCCMActions.ps1
File metadata and controls
455 lines (425 loc) · 16.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<#
.SYNOPSIS
Initiate SCCM actions
.DESCRIPTION
This script will initiate SCCM actions and wait until the action is complete before continuing.
.EXAMPLE
powershell.exe -executionpolicy bypass -file SCCMActions.ps1
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.122
Created on: 5/20/2016 2:28 PM
Created by: Mick Pletcher
Filename: SCCMActions.ps1
===========================================================================
#>
[CmdletBinding()]
param ()
function Get-CurrentDate {
<#
.SYNOPSIS
Get the current date and return formatted value
.DESCRIPTION
Return the current date in the following format: mm-dd-yyyy
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([string])]
param ()
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToShortDateString()
$CurrentDate = $CurrentDate -replace "/", "-"
If ($CurrentDate[2] -ne "-") {
$CurrentDate = $CurrentDate.Insert(0, "0")
}
If ($CurrentDate[5] -ne "-") {
$CurrentDate = $CurrentDate.Insert(3, "0")
}
Return $CurrentDate
}
function Invoke-ApplicationDeploymentEvaluationCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Application Deployment Evaluation Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000121"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\DCMReporting.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If (($Log -like "*FinalRelease*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-DiscoveryDataCollectionCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Discovery Data Collection Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000003"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\InventoryAgent.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If (($Log -like "*End of message processing*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-FileCollectionCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running File Collection Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000010"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\InventoryAgent.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If ((($Log -like "*Action completed*") -or ($Log -like "*Exiting as no items to collect*")) -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-HardwareInventoryCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Hardware Inventory Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000001"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\InventoryAgent.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If (($Log -like "*End of message processing*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If (($Log -like "*already in queue. Message ignored.*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Ignored" -ForegroundColor Red
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-MachinePolicyEvaluationCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %r
$StartTime = $StartTime.Split(' ')
$StartTime = $StartTime[0]
$StartTime = $StartTime.Substring(0, $StartTime.Length - 3)
$CurrentDate = Get-CurrentDate
Write-Host "Running Machine Policy Evaluation Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000022"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\PolicyEvaluator.log"
$Count = $Log.count
$LogTime = $Log[$Count - 6]
$LogTime = $LogTime.Split('"')
$LogTime = $LogTime[1]
$LogTime = [management.managementdatetimeconverter]::ToDateTime($LogTime)
$LogDate = $LogTime
$LogTime = $LogTime.ToShortTimeString()
$LogTime = $Logtime.Split(' ')
$LogTime = $LogTime[0]
If ($LogTime[2] -ne ":") {
$LogTime = $LogTime.Insert(0, "0")
}
$LogDate = $LogDate.ToShortDateString()
$LogDate = Get-CurrentDate
$LogStatus = $Log[$Count - 9]
If (($LogStatus -like "*instance of CCM_PolicyAgent_PolicyEvaluationComplete*") -and ($CurrentDate -eq $LogDate) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-MachinePolicyRetrievalCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %r
$StartTime = $StartTime.Split(' ')
$StartTime = $StartTime[0]
$StartTime = $StartTime.Substring(0, $StartTime.Length - 3)
$CurrentDate = Get-CurrentDate
Write-Host "Running Machine Policy Retrieval Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000021"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\PolicyEvaluator.log"
$Count = $Log.count
$LogTime = $Log[$Count - 6]
$LogTime = $LogTime.Split('"')
$LogTime = $LogTime[1]
$LogTime = [management.managementdatetimeconverter]::ToDateTime($LogTime)
$LogDate = $LogTime
$LogTime = $LogTime.ToShortTimeString()
$LogTime = $Logtime.Split(' ')
$LogTime = $LogTime[0]
If ($LogTime[2] -ne ":") {
$LogTime = $LogTime.Insert(0, "0")
}
$LogDate = $LogDate.ToShortDateString()
$LogDate = Get-CurrentDate
$LogStatus = $Log[$Count - 9]
If (($LogStatus -like "*instance of CCM_PolicyAgent_PolicyEvaluationComplete*") -and ($CurrentDate -eq $LogDate) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-SoftwareInventoryCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Software Inventory Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000002"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\InventoryAgent.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If (($Log -like "*Initialization completed in*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-SoftwareMeteringUsageReportCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Software Metering Usage Report Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000031"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\SWMTRReportGen.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If ((($Log -like "*No usage data found to generate software metering report*") -or ($Log -like "*Successfully generated report header*") -or ($Log -like "*Message ID of sent message*")) -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-SoftwareUpdatesDeploymentEvaluationCycle {
<#
.SYNOPSIS
Scan for software updates that are out of compliance
.DESCRIPTION
Initiates a scan for software updates compliance. Before client computers can scan for software update compliance, the software updates environment must be configured.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param ()
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Software Updates Deployment Evaluation Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000108"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\ScanAgent.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If (($Log -like "*Calling back to client on Scan request complete*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-SoftwareUpdatesScanCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Software Updates Scan Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000113"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\scanagent.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If (($Log -like "*scan completion received*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Invoke-WindowsInstallerSourceListUpdateCycle {
$Completed = $false
$StartTime = Get-Date -UFormat %R
$CurrentDate = Get-CurrentDate
Write-Host "Running Windows Installer Source List Update Cycle....." -NoNewline
Start-ConfigurationManagerClientScan -ScheduleID "00000000-0000-0000-0000-000000000032"
Do {
Start-Sleep -Seconds 1
$CurrentTime = Get-Date -UFormat %R
$TimeDifference = New-TimeSpan -Start $StartTime -End $CurrentTime
$Log = Get-Content $env:windir"\ccm\logs\SrcUpdateMgr.log"
$Count = $Log.count
$Count = $Count - 1
$Log = $Log[$Count]
$LogTable = $Log.split("<")[-1]
$LogTable = $LogTable.Substring(0, $LogTable.length - 1) -replace ' ', ';'
$LogTable = "@{$($LogTable)}" | Invoke-Expression
$LogTime = $LogTable.time.Substring(0, 5)
[datetime]$StringTime = $LogTable.time
If (($Log -like "*MSI update source list task finished successfully*") -and ($CurrentDate -eq $LogTable.date) -and ($LogTime -ge $StartTime)) {
Write-Host "Completed" -ForegroundColor Yellow
$Completed = $true
}
If ($TimeDifference.Minutes -ge 5) {
Write-Host "Failed" -ForegroundColor Yellow
$Completed = $true
}
} while ($Completed -eq $false)
}
function Start-ConfigurationManagerClientScan {
<#
.SYNOPSIS
Initiate Configuration Manager Client Scan
.DESCRIPTION
This will initiate an SCCM action
.PARAMETER ScheduleID
GUID ID of the SCCM action
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateSet('00000000-0000-0000-0000-000000000121', '00000000-0000-0000-0000-000000000003', '00000000-0000-0000-0000-000000000010', '00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000021', '00000000-0000-0000-0000-000000000022', '00000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-000000000031', '00000000-0000-0000-0000-000000000108', '00000000-0000-0000-0000-000000000113', '00000000-0000-0000-0000-000000000111', '00000000-0000-0000-0000-000000000026', '00000000-0000-0000-0000-000000000027', '00000000-0000-0000-0000-000000000032')]$ScheduleID
)
$WMIPath = "\\" + $env:COMPUTERNAME + "\root\ccm:SMS_Client"
$SMSwmi = [wmiclass]$WMIPath
$Action = [char]123 + $ScheduleID + [char]125
[Void]$SMSwmi.TriggerSchedule($Action)
}
Clear-Host
Invoke-SoftwareUpdatesScanCycle
Invoke-SoftwareUpdatesDeploymentEvaluationCycle
Invoke-ApplicationDeploymentEvaluationCycle
Invoke-DiscoveryDataCollectionCycle
Invoke-FileCollectionCycle
Invoke-HardwareInventoryCycle
Invoke-MachinePolicyEvaluationCycle
Invoke-MachinePolicyRetrievalCycle
Invoke-SoftwareInventoryCycle
Invoke-SoftwareMeteringUsageReportCycle
Invoke-WindowsInstallerSourceListUpdateCycle