/
ClientHealthCheck.ps1
789 lines (728 loc) · 25.3 KB
/
ClientHealthCheck.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<#
.SYNOPSIS
SCCM client health check and repair
.DESCRIPTION
This script will check components used by the SCCM client
and if an issue is found it will attempt to fix the issue.
1. Looks to see if the client is installed, if not it
will install the client from the specified location.
2. Next the script will check the startup type and status
of services needed for SCCM
3. Runs specific client actions to verify they are working
4. Checks to see if basic WMI queries are working and that
the SCCM name space is working.
5. If an issue is found that can be resolved by reinstalling
the SCCM client this will happen last. The script will also
look for any remaining client files and remove them to be sure
there are no corrupted files left behind.
.NOTES
Created By: Kris Gross
Contact: Krisgross@sccmtst.com
Twitter: @kmgamd
Version 1.1.2.2
.LINK
You can get updates to this script and others from here
http://www.sccmtst.com/
#>
### Variables that can be changed ###
# Location of the SCCM Client install files
$SCCMClientLocation = ""
#Managment Point Server
$SMSMP = ""
# Domain DNS Suffix
$DNSSUFFIX = ""
# site code
$SITECODE = ""
# Log file the script writes to
$LogFile = "$ENV:windir\SCCMClientHealthCheck.log"
# Allows the log file to age for specified amount of days
$LogAge = "31"
#################################################
### Not recomended to change these variables ###
$CCMPath = "$ENV:windir\CCM"
$ScriptName = $MyInvocation.MyCommand.Name
$Computername = $ENV:COMPUTERNAME
################################################
# Clears log if its larger then 5MB or older then the specified amount of days #
function Get-LogFileSize
{
$LogFileSize = (Get-Item -path $LogFile).Length
$LogFileAge = (Get-Item -Path $LogFile).CreationTime
$AcceptableDate = (Get-Date).AddDays(-"$LogAge")
If (($LogFileSize -ge 5000001) -or ($LogFileAge -le $AcceptableDate))
{
Remove-Item $LogFile -Force
$Global:LogCleaned = "True"
}
}
# Creates a new log file for the script #
function New-LogFile
{
$LogFilePaths = "$LogFile"
Foreach ($LogFilePath in $LogFilePaths)
{
$script:NewLogError = $null
$script:ConfigMgrLogFile = $LogFilePath
Add-LogEntry "********************************************************************************************************************" "1"
Add-LogEntry "Starting SCCM Client Health Check" "1"
If ($Global:LogCleaned -EQ "True") {Add-LogEntry "Log was cleaned due to being too large or older then $LogAge Days"}
If (-Not($script:NewLogError)) { break }
}
If ($script:NewLogError)
{
$script:Returncode = 1
Exit $script:Returncode
}
}
# Logs the status of the script in a CMtrace format #
function Add-LogEntry ($LogMessage, $Messagetype)
{
# Date and time is set to the CMTrace standard
# The Number after the log message in each function corisponts to the message type
# 1 is info
# 2 is a warning
# 3 is a error
Add-Content $script:ConfigMgrLogFile "<![LOG[$LogMessage]LOG]!><time=`"$((Get-Date -format HH:mm:ss)+".000+300")`" date=`"$(Get-Date -format MM-dd-yyyy)`" component=`"$ScriptName`" context=`"`" type=`"$Messagetype`" thread=`"`" file=`"powershell.exe`">" -Errorvariable script:NewLogError
}
# Closes the log file and exits the script #
function Exit-Script()
{
Remove-Item env:SEE_MASK_NOZONECHECKS
Add-LogEntry "Closing the log file for $ScriptName." "1"
Add-LogEntry "********************************************************************************************************************" "1"
Exit $script:Returncode
}
# Check workstation Compatibility
function Check-Compatibility
{
Add-LogEntry "---------------------------" "1"
#Checking PowerShell Version
Add-LogEntry "Checking PowerShell Version" "1"
$PowerShellVersion = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine").PowerShellVersion
IF ($PowerShellVersion -le "5.1")
{
Add-LogEntry "PowerShell Version: $PowerShellVersion" "1"
Add-LogEntry "ERROR: PowerShell Version is less then 5.1" "3"
Add-LogEntry "ACTION: Install Windows Management Framework 5.1" "1"
Exit-Script
}
}
# Check if SCCM Client is installed #
Function Get-ClientInstalled
{
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Checking if SCCM Client is installed" "1"
If ((Get-WmiObject win32_Product | Where-Object Name -EQ "Configuration Manager Client") -and (Get-Service -Name ccmexec))
{
Add-LogEntry "SCCM Client is installed" "1"
}
Else
{
Add-LogEntry "WARNING: SCCM Clinet is not installed" "2"
Add-LogEntry "SCCM Client will be installed after other checks have been completed" "1"
$Global:InstallClient = "True"
}
}
# Installs SCCM Client #
Function Install-SCCMClient
{
Add-LogEntry "---------------------------" "1"
IF ($Global:InstallClient -eq "True")
{
IF (Test-path -Path $CCMPath)
{
Add-LogEntry "SCCM client install files found" "1"
Add-LogEntry "Removing old client install files" "1"
Remove-Item -Path C:\Windows\ccmsetup -Recurse -Force
start-sleep -Seconds 10
IF (Test-Path -Path C:\Windows\ccmsetup) {Add-LogEntry "WARNING: Could not remove ccmsetup folder" "2"}
Add-LogEntry "Running SCCM Client uninstall" "1"
$UninstallClient = @"
@ECHO off
ECHO Uninstalling the old client...Please Wait
%1\ccmsetup.exe /Uninstall
:start
tasklist /FI "IMAGENAME eq ccmsetup.exe" | find /i "ccmsetup.exe" >> null
IF ERRORLEVEL 2 GOTO running
IF ERRORLEVEL 1 GOTO end
:running
goto start
:end
ECHO uninstall Complete
exit cmd.exe
"@
Add-Content $Env:TEMP\UninstallClient.bat $UninstallClient
Invoke-Expression "$Env:TEMP\UninstallClient.bat $SCCMClientLocation"
Remove-Item "$Env:TEMP\UninstallClient.bat"
If (Get-WmiObject win32_Product | Where-Object Name -EQ "Configuration Manager Client")
{
Add-LogEntry "ERROR: Could not uninstall SCCM Client" "3"
}
If (!(Get-WmiObject win32_Product | Where-Object Name -EQ "Configuration Manager Client"))
{
Add-LogEntry "SCCM Client successfully uninstalled" "1"
}
Add-LogEntry "Remove leftover client files"
Start-Sleep -Seconds 60
Remove-Item C:\Windows\ccmcache -Recurse -Force
Start-Sleep -Seconds 10
IF (Test-Path -Path C:\Windows\ccmcache) { Add-LogEntry "Could not remove ccmcache folder" "2"}
Remove-Item $CCMPath -Recurse -Force
Start-Sleep -Seconds 10
IF (Test-Path -Path $CCMPath) { Add-LogEntry "Could not remove CCM folder" "2"}
}
Add-LogEntry "Running SCCM Client Install" "1"
IF (!(Test-Path $SCCMClientLocation))
{
Add-LogEntry "ERROR: Cannont find $SCCMClientLocation" "3"
Exit-Script
}
$InstallClientScript = @"
@ECHO off
ECHO Installing the new client...Please Wait
%1\ccmsetup.exe SMSMP=$SMSMP DNSSUFFIX=$DNSSUFFIX SMSSITECODE=$SITECODE CCMLOGLEVEL=0 CCMLOGMAXSIZE=16000000 CCMLOGMAXHISTORY=1 CCMDEBUGLOGGING=0 SMSCACHSIZE=25600
:start
tasklist /FI "IMAGENAME eq ccmsetup.exe" | find /i "ccmsetup.exe" >> null
IF ERRORLEVEL 2 GOTO running
IF ERRORLEVEL 1 GOTO end
:running
goto start
:end
ECHO Install Complete
exit cmd.exe
"@
Add-Content $Env:TEMP\InstallClient.bat $InstallClientScript
Invoke-Expression "$Env:TEMP\InstallClient.bat $SCCMClientLocation"
Remove-Item "$Env:TEMP\InstallClient.bat"
If (!(Get-WmiObject win32_Product | Where-Object Name -EQ "Configuration Manager Client"))
{
Add-LogEntry "ERROR: Could not install SCCM Client" "3"
}
If (Get-WmiObject win32_Product | Where-Object Name -EQ "Configuration Manager Client")
{
Add-LogEntry "SCCM Client successfully installed" "1"
Add-LogEntry "Running Machine Policy Cycle"
$SMSClient = [wmiclass] "\\$env:COMPUTERNAME\root\ccm:SMS_Client"
$SMSClient.TriggerSchedule("{00000000-0000-0000-0000-000000000021}")
$Global:InstallClient = "False"
}
If ($Global:CheckWMI -eq "True")
{
if(Get-WmiObject -Namespace root\ccm -Class sms_client)
{
Add-LogEntry "SUCCESS: SCCM WMI was repaired" "1"
}
else
{
Add-LogEntry "ERROR: SCCM WMI was not repaired" "3"
}
}
}
}
# Check if dependent services are running and set them to correct startup type #
Function Get-DependentServices
{
If ($Global:InstallClient -ne "True")
{
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Checking startup type of CcmExec service" "1"
IF ((Get-Service "CcmExec").StartType -ne "Automatic")
{
Add-LogEntry "WARNING: CcmExec service needs to be set to Automatic" "2"
Add-LogEntry "Attempting to change start type to Automatic" "1"
try {
Set-Service "CcmExec" -StartupType "Automatic" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
IF ((Get-Service "CcmExec").StartType -ne "Automatic"){Add-LogEntry "ERROR: Could not change start type" "3"}
IF ((Get-Service "CcmExec").StartType -eq "Automatic"){Add-LogEntry "SUCCESS: CcmExec service start type was set to Automatic" "1"}
}
else
{
Add-LogEntry "CcmExec service startup type is correct" "1"
}
Add-LogEntry "Checking status of Ccmexec service" "1"
if((Get-Service -Name "ccmexec").Status -eq "Stopped")
{
Add-LogEntry "WARNING: CCMExec service stopped" "2"
Add-LogEntry "Attempting to startng CCMExec service " "1"
try {
Start-Service -Name CcmExec -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
start-Sleep -Seconds 10
if((Get-Service -Name "ccmexec").Status -eq "Stopped"){Add-LogEntry "ERROR: Could not start CCMExec service" "3"}
if((Get-Service -Name "ccmexec").Status -ne "Stopped"){Add-LogEntry "SUCCESS: started CcmExec service"}
}
Else
{
Add-LogEntry "Ccmexec service is running" "1"
}
}
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Checking startup type of BITS service" "1"
IF ((Get-Service "BITS").StartType -ne "Automatic")
{
Add-LogEntry "WARNING: BITS service needs to be set to Automatic" "2"
Add-LogEntry "Attempting to change startup type to Automatic" "1"
try {
Set-Service "BITS" -StartupType "Automatic" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
IF ((Get-Service "BITS").StartType -ne "Automatic"){Add-LogEntry "ERROR: Could not change startup type" "3"}
IF ((Get-Service "BITS").StartType -eq "Automatic"){Add-LogEntry "SUCCESS: BITS service startup type was set to Automatic"}
}
else
{
Add-LogEntry "BITS service startup is set correctly" "1"
}
Add-LogEntry "Checking status of BITS service" "1"
if((Get-Service -Name "BITS").status -eq "Stopped")
{
Add-LogEntry "WARNING: BITS service Stopped" "2"
Add-LogEntry "Attempting to start BITS service" "1"
try {
Start-Service -Name "BITS" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
start-Sleep -Seconds 10
if((Get-Service -Name "BITS").status -eq "Stopped"){Add-LogEntry "ERROR: Could not start BITS service" "3"}
if((Get-Service -Name "BITS").status -ne "Stopped"){Add-LogEntry "SUCCESS: started BITS service" "1"}
}
else
{
Add-LogEntry "BITS service is started" "1"
}
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Checking startup type of wuauserv service" "1"
IF ((Get-Service "wuauserv").StartType -ne "Manual")
{
Add-LogEntry "WARNING: Wuauserv service needs to be set to Manual" "2"
Add-LogEntry "Attempting to change start type to Manual" "1"
try {
Set-Service "wuauserv" -StartupType "Manual" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
start-Sleep -Seconds 10
IF ((Get-Service "wuauserv").StartType -ne "Manual"){Add-LogEntry "ERROR: Could not change start type" "3"}
IF ((Get-Service "wuauserv").StartType -eq "Manual"){Add-LogEntry "SUCCESS: wuauserv service start type was set to Manual"}
}
else
{
Add-LogEntry "Wuauserv service startup type is correct" "1"
}
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Checking startup type of Winmgmt service" "1"
IF ((Get-Service "Winmgmt").StartType -ne "Automatic")
{
Add-LogEntry "WARNING: Winmgmt service needs to be set to Automatic" "2"
Add-LogEntry "Attempting to change start type to Automatic" "1"
try {
Set-Service "Winmgmt" -StartupType "Automatic" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
start-Sleep -Seconds 10
IF ((Get-Service "Winmgmt").StartType -ne "Automatic"){Add-LogEntry "ERROR: Could not change start type" "3"}
IF ((Get-Service "Winmgmt").StartType -eq "Automatic"){Add-LogEntry "SUCCESS: Winmgmt service startup type set to Automatic"}
}
else
{
Add-LogEntry "Winmgmt service startuptype correctly set" "1"
}
Add-LogEntry "Checking status of Winmgmt service" "1"
if((Get-Service -Name "Winmgmt").status -eq "Stopped")
{
Add-LogEntry "WARNING: Winmgmt service Stopped" "2"
Add-LogEntry "Attempting to start Winmgmt service" "1"
try {
Start-Service -Name "Winmgmt" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
start-Sleep -Seconds 10
if((Get-Service -Name "Winmgmt").status -eq "Stopped"){Add-LogEntry "ERROR: Could not start Winmgmt service" "3"}
if((Get-Service -Name "Winmgmt").status -ne "Stopped"){Add-LogEntry "SUCCESS: Winmgmt service started" "1"}
}
else
{
Add-LogEntry "Winmgmt service is running" "1"
}
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Checking startup type of RemoteRegistry service" "1"
IF ((Get-Service "RemoteRegistry").StartType -ne "Automatic")
{
Add-LogEntry "WARNING: RemoteRegistry service needs to be set to Automatic" "2"
Add-LogEntry "Attempting to change startup type to Automatic" "1"
try {
Set-Service "RemoteRegistry" -StartupType "Automatic" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
start-Sleep -Seconds 10
IF ((Get-Service "RemoteRegistry").StartType -ne "Automatic"){Add-LogEntry "ERROR: Could not change start type" "3"}
IF ((Get-Service "RemoteRegistry").StartType -eq "Automatic"){Add-LogEntry "SUCCESS: RemoteRegistry service start type set to Automatic"}
}
else
{
Add-LogEntry "RemoteRegistry service startup type correctly set" "1"
}
Add-LogEntry "Checking status of RemoteRegistry service" "1"
if((Get-Service -Name "RemoteRegistry").status -eq "Stopped")
{
Add-LogEntry "WARNING: RemoteRegistry service Stopped" "2"
Add-LogEntry "Attempting to start RemoteRegistry service" "1"
try {
Start-Service -Name "RemoteRegistry" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
start-Sleep -Seconds 10
if((Get-Service -Name "RemoteRegistry").status -eq "Stopped"){Add-LogEntry "ERROR: Could not start RemoteRegistry service" "3"}
if((Get-Service -Name "RemoteRegistry").status -eq "Running"){Add-LogEntry "SUCCESS: started RemoteRegistry service" "1"}
}
else
{
Add-LogEntry "RemoteRegistry service started" "1"
}
}
# check if cycles are working #
Function Get-ClientActionsStatus
{
IF ($Global:InstallClient -ne "True")
{
Add-LogEntry "---------------------------" "1"
$MachinePolicyRetrievalEvaluation = "{00000000-0000-0000-0000-000000000021}"
$SoftwareUpdatesDeployment = "{00000000-0000-0000-0000-000000000108}"
$ApplicationDeployment = "{00000000-0000-0000-0000-000000000121}"
If (Get-WmiObject win32_Product | Where-Object Name -EQ "Configuration Manager Client")
{
$machine_status = Invoke-WmiMethod -Namespace root\ccm -Class sms_client -Name TriggerSchedule $MachinePolicyRetrievalEvaluation
IF($machine_status)
{
Add-LogEntry "Machine Policy Retrieval Evaluation Action is working correctly" "1"
}
IF (!($machine_status))
{
Add-LogEntry "WARNING: Machine Policy Retrieval Evaluation Action is not working correctly" "2"
Add-LogEntry "This will be resolved by reinstalling the SCCM Client"
$Global:InstallClient = "True"
}
$SoftwareUpdate_status = Invoke-WmiMethod -Namespace root\ccm -Class sms_client -Name TriggerSchedule $SoftwareUpdatesDeployment
IF($SoftwareUpdate_status)
{
Add-LogEntry "Software Update Deployment Action is working correctly" "1"
}
IF(!($softwareUpdate_status))
{
Add-LogEntry "WARNING: Software Update Deployment Action is not working correctly" "2"
Add-LogEntry "This will be resolved by reinstalling the SCCM Client"
$Global:InstallClient = "True"
}
$ApplicationDeployment_Status = Invoke-WmiMethod -Namespace root\ccm -Class sms_client -Name TriggerSchedule $ApplicationDeployment
IF($ApplicationDeployment_Status)
{
Add-LogEntry "Application Deployment Action is working correctly"
}
IF(!($ApplicationDeployment_Status))
{
Add-LogEntry "WARNING: Application Deployment Action is not working correctly" "2"
Add-LogEntry "This will be resolved by reinstalling the SCCM Client"
$Global:InstallClient = "True"
}
}
}
}
# Checks if WMI is working correctly #
Function Get-WMIStatus
{
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Checking status of WMI" "1"
If ($Global:InstallClient -ne "True")
{
Add-LogEntry "SCCM Client set to be reinstalled, will not check SCCM WMI" "1"
$WMIStatus = "Good"
try {
Get-WmiObject win32_ComputerSystem -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR:An Issue with win32_ComputerSystem occured; $_" "3"
$WMIStatus = "Bad"
}
try {
Get-WmiObject win32_OperatingSystem -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR:An Issue with win32_OperatingSystem occured; $_" "3"
$WMIStatus = "Bad"
}
try {
Get-WmiObject win32_Service -ErrorAction stop
}
catch {
Add-LogEntry "ERROR:An Issue with win32_Service occured; $_" "3"
$WMIStatus = "Bad"
}
IF ($WMIStatus -eq "Good")
{
Add-LogEntry "Checking system and SCCM components of WMI" "1"
try
{
Get-WmiObject -Namespace root\ccm -Class sms_client -ErrorAction Stop
$WMIStatus = "Good"
}
catch
{
Add-LogEntry "ERROR:An issue with sms_client WMI occured; $_" "3"
}
}
}
if($WMIStatus -eq "Good")
{
Add-LogEntry "WMI Seems to be working correctly" "1"
}
else
{
Add-LogEntry "WARNING: One or more WMI classes are corrupted" "2"
IF ($Global:InstallClient -ne "True") {Add-LogEntry "WARNING: SCCM Client will need to be reinstalled" "2"}
Add-LogEntry "Attempting to repair WMI" "1"
$DependentServices = Get-Service winmgmt -DependentServices | Where-Object Status -eq "Running"
Foreach ($Service in $DependentServices)
{
IF ((Get-Service $Service).Status -eq "Running")
{
Add-LogEntry "Attempting to stop $Service service" "1"
try {
Stop-Service "$Service" -Force -ErrorAction Stop
}
catch {
Add-LogEntry "Error: $_" "3"
}
Start-Sleep 10
IF ((Get-Service $Service).Status -eq "Running")
{
Add-LogEntry "ERROR: Could not stop $Service service" "3"
Add-LogEntry "It is not recommened to continue with WMI repair proccess, Stopping Script" "2"
Exit-Script
}
}
}
IF ((Get-Service Winmgmt).Status -eq "Running")
{
Add-LogEntry "Attempting to stop winmgmt service" "1"
try {
Stop-Service "winmgmt" -Force -ErrorAction Stop
}
catch {
Add-LogEntry "Error: $_" "3"
}
Start-Sleep -Seconds 10
IF ((Get-Service Winmgmt).Status -eq "Running")
{
Add-LogEntry "ERROR: Could not stop winmgmt service" "3"
Add-LogEntry "It is not recommened to continue with WMI repair proccess, Stopping Script" "2"
Add-LogEntry "ACTION: Try to stop the Windows Management Instrumentation service manully if you cannont the computer will need to be reimaged"
Exit-Script
}
}
IF ((Get-Service CcmExec).Status -eq "Running")
{
Add-LogEntry "Attempting to stop CcmExec service" "1"
try {
Stop-Service "CcmExec" -Force -ErrorAction Stop
}
catch {
}
Start-Sleep -Seconds 10
IF ((Get-Service CcmExec).Status -eq "Running")
{
Add-LogEntry "ERROR: Could not stop CcmExec service" "3"
Add-LogEntry "It is not recommened to continue with WMI repair proccess, Stopping Script" "2"
Add-LogEntry "ACTION: try to stop SMS Agent Host service manully if you cannont uninstall the SCCM client and run the script again" "2"
Exit-Script
}
}
IF ((Get-Service wmiApSrv).Status -eq "Running")
{
Add-LogEntry "Attempting to stop wmiApSrv service" "1"
try {
Stop-Service "wmiApSrv" -Force -ErrorAction Stop
}
catch {
Add-LogEntry "Error: $_" "3"
}
Start-Sleep -Seconds 10
IF ((Get-Service wmiApSrv).Status -eq "Running")
{
Add-LogEntry "ERROR: Could not stop wmiApSrv service" "3"
Add-LogEntry "It is not recommened to continue with WMI repair proccess, Stopping Script" "2"
Exit-Script
}
}
Add-LogEntry "All Services stopped, Repairing WMI" "1"
& ($ENV:SystemRoot+"\system32\wbem\winmgmt.exe") /resetrepository
If ($LASTEXITCODE -eq 1) {Add-LogEntry "ERROR:Could not run winmgmt.exe /resetrepository; $_" "3"}
& ($ENV:SystemRoot+"\system32\wbem\winmgmt.exe") /salvagerepository
If ($LASTEXITCODE -eq 1) {Add-LogEntry "ERROR:Could not run winmgmt.exe /salvagerepository; $_" "3"}
Add-LogEntry "Completed running the repaire process" "1"
Add-LogEntry "Attempting to restart services needed for WMI" "1"
Add-LogEntry "Attempting to restart Winmgmt service" "1"
Foreach ($Service in $DependentServices)
{
Add-LogEntry "Attempting to restart $Service service" "1"
try {
Start-Service $Service -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
Start-Sleep -Seconds 5
IF ((Get-Service "$Service").Status -eq "Stopped")
{
Add-LogEntry "ERROR: Could not restart $Service" "3"
Add-LogEntry "Attempting to restart $Service service in 10 seconds"
Start-Sleep -Seconds 10
Start-Service $Service
IF ((Get-Service "$Service").Status -eq "Running") {Add-LogEntry "SUCCESS: $Service service is now running" "1"}
}
else
{
Add-LogEntry "SUCCESS: $Service service is now running" "1"
}
}
try {
Start-Service "Winmgmt" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
Start-Sleep -Seconds 5
IF ((Get-Service Winmgmt).Status -eq "Stopped")
{
Add-LogEntry "ERROR: Could not restart winmgmt service" "3"
Add-LogEntry "Attempting to restart winmgmt service in 10 seconds" "1"
Start-Sleep -Seconds 10
Start-Service "Winmgmt"
IF ((Get-Service Winmgmt).Status -eq "Running")
{
Add-LogEntry "SUCCESS: Winmgmt service is now running" "1"
}
Else
{
Add-LogEntry "ERROR: Still Could not start winmgmt service" "3"
Add-LogEntry "ACTION: try to restart Windows Management Instrumentation service manully if you are unable to computer will need to me reimaged" "2"
}
}
Else
{
Add-LogEntry "SUCCESS: Winmgmt service is now running" "1"
}
Add-LogEntry "Attempting to restart wmiApSrv service" "1"
try {
Start-Service "wmiApSrv" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
Start-Sleep -Seconds 5
IF ((Get-Service wmiApSrv).Status -eq "Stopped")
{
Add-LogEntry "ERROR: Could not restart wmiapSrv service" "3"
Add-LogEntry "Attempting to restart wimApSrv service in 10 seconds" "1"
Start-Sleep -Seconds 10
Start-Service "wmiApSrv"
IF ((Get-Service wmiApSrv).Status -eq "Running")
{
Add-LogEntry "SUCCESS: wmiApSrv service is now running" "1"
}
}
else
{
Add-LogEntry "SUCCESS: wmiApSrv service is now running" "1"
}
Add-LogEntry "Attempting to restart WmiPrvSE service" "1"
try {
Start-Service "WmiPrvSE" -ErrorAction Stop
}
catch {
Add-LogEntry "ERROR: $_" "3"
}
Start-Sleep -Seconds 5
IF ((Get-Service WmiPrvSE).Status -eq "Stopped")
{
Add-LogEntry "ERROR: Could not restart WmiPrvSE service" "3"
Add-LogEntry "Attempting to restart WmiPrvSE service in 10 seconds" "1"
Start-Sleep -Seconds 10
Start-Service "WmiPrvSE"
IF ((Get-Service WmiPrvSE).Status -eq "Running") {Add-LogEntry "SUCCESS: WmiPrvSE service is now running" "1"}
}
else
{
Add-LogEntry "SUCCESS: WmiPrvSE service is now running" "1"
}
$Global:InstallClient = "True"
try
{
Get-WmiObject win32_ComputerSystem -ErrorAction Stop
Get-WmiObject win32_OperatingSystem -ErrorAction Stop
Get-WmiObject win32_Service -ErrorAction Stop
Add-LogEntry "SUCCESS: Standard WMI has been repaired" "1"
Add-LogEntry "Will Check SCCM WMI after client install" "1"
Add-LogEntry "Reinstalling the SCCM Client to repair the SCCM part of WMI" "1"
$Global:CheckWMI = "True"
}
catch
{
Add-LogEntry "ERROR: Standard WMI was not repaired - $_" "3"
Add-LogEntry "ACTION: This device needs to be reimaged" "2"
Exit-Script
}
}
}
# Counts number of items in computer temp folder #
Function Get-TempFiles
{
Add-LogEntry "---------------------------" "1"
Add-LogEntry "Gathering Temp file count"
Add-LogEntry "More then 60000 items can be problematic for the SCCM client"
$TempCount = ( Get-ChildItem c:\windows\temp -recurse -Force | Measure-Object ).Count
IF ($TempCount -ge "60000")
{
Add-LogEntry "WARNING: $TempCount items found" "2"
Add-LogEntry "Removeing temp items" "1"
Remove-Item C:\Windows\Temp\* -Recurse -Force -ErrorAction SilentlyContinue
}
Else
{
Add-LogEntry "$TempCount items found" "1"
Add-LogEntry "Nothing needs to be removed"
}
}
####################################
# Runs the functions of the script #
# If you need to only run part of the script comment out the functions here you dont need
Get-LogFileSize
New-LogFile
Check-Compatibility
Get-ClientInstalled
Get-DependentServices
# This function is disabled by defuelt, Running a manual proccess to clean the temp folder too often is not supported by Microsoft
#Get-TempFiles
Get-ClientActionsStatus
Get-WMIStatus
Install-SCCMClient
Exit-Script