-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigMgr_LogFile_Opener.ps1
More file actions
1938 lines (1702 loc) · 71.4 KB
/
Copy pathConfigMgr_LogFile_Opener.ps1
File metadata and controls
1938 lines (1702 loc) · 71.4 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
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Provides simple access to the ConfigMgr Client Logs using CMTrace or CMLogViewer
.DESCRIPTION
Provides simple access to the ConfigMgr Client Logs using CMTrace or CMLogViewer
.PARAMETER CMTrace
Specify the Path to CMTrace.exe
.PARAMETER CMLogViewer
Specify the Path to CMLogViewer.exe
.PARAMETER OneTrace
Specify the Path to CMOneTrace.exe
.PARAMETER Hostname
Specify a Default hostname for direct connection. Otherwise the Tool will prompt you to specify a hostname.
.PARAMETER ClientLogFilesDir
Specify the directory in which the ConfigMgr Client LogFiles are located. (e.g: 'Program Files\CCM\Logs')
.PARAMETER ActionDelayShort
Specify the amount of time in milliseconds, the Script should wait between the Steps when opening multiple LogFiles in GUI Mode. Default value is 1500
.PARAMETER ActionDelayLong
Specify the amount of time in milliseconds, the Script should wait between the Steps when opening multiple LogFiles in GUI Mode. Default value is 2500
.PARAMETER LogProgram
Specify which Log Program should be used when the tool is starting. Default value is 'CMTrace'
.PARAMETER LogProgramWindowStyle
Specify the Window Style of CMTrace and File Explorer. Default value is 'normal'
.PARAMETER DisableHistoryLogFiles
If specified, the Tool won't open any history log files. Opening history log files is currently only supported with CMLogViewer.
.PARAMETER RecentLogLimit
Specify the number of recent log files which will be listed in the menu. Default value is 15
.PARAMETER DisableUpdater
If specified, the Tool won't prompt if there is a newer Version available
.PARAMETER EnableAutoLogLaunch
If specified, the Tool will automatically open the corresponding logs when executing client actions.
.EXAMPLE
.\ConfigMgr_LogFile_Opener.ps1 -CMTrace 'C:\temp\CMTrace.exe' -Hostname 'PC01' -ClientLogFilesDir 'Program Files\CCM\Logs' -LogProgramWindowStyle Maximized
.\ConfigMgr_LogFile_Opener.ps1 -CMLogViewer 'C:\temp\CMLogViewer.exe' -Hostname 'PC02' -DisableHistoryLogFiles -LogProgram CMLogViewer -RecentLogLimit 25
.NOTES
Script name: ConfigMgr_LogFile_Opener.ps1
Author: @SimonDettling <msitproblog.com>
Date modified: 2023-05-22
Version: 3.0.6
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false, HelpMessage = 'Specify the hostname for direct connection. Otherwise the Tool will prompt you to specify a hostname.')]
[String] $Hostname = '',
[Parameter(Mandatory = $false, HelpMessage = 'Specify the Path to CMTrace.exe')]
[String] $CMTrace = 'C:\Windows\CCM\CMTrace.exe',
[Parameter(Mandatory = $false, HelpMessage = 'Specify the Path to CMLogViewer.exe')]
[String] $CMLogViewer = 'C:\Program Files (x86)\Configuration Manager Support Center\CMLogViewer.exe',
[Parameter(Mandatory = $false, HelpMessage = 'Specify the Path to CMOneTrace.exe')]
[String] $OneTrace = 'C:\Program Files (x86)\Configuration Manager Support Center\CMOneTrace.exe',
[Parameter(Mandatory = $false, HelpMessage = 'Specify the directory in which the ConfigMgr Client Logfiles are located. (e.g: "Program Files\CCM\Logs")')]
[String] $ClientLogFilesDir = 'C$\Windows\CCM\Logs',
[Parameter(Mandatory = $false, HelpMessage = 'Specify the amount of time in milliseconds, the Script should wait between the Steps when opening multiple LogFiles in GUI Mode. Default value is 1500')]
[Int] $ActionDelayShort = 1700,
[Parameter(Mandatory = $false, HelpMessage = 'Specify the amount of time in milliseconds, the Script should wait between the Steps when opening multiple LogFiles in GUI Mode. Default value is 2500')]
[Int] $ActionDelayLong = 3500,
[Parameter(Mandatory = $false, HelpMessage = "Specify which Log Program should be active when the tool is starting. Default value is 'CMTrace'")]
[ValidateSet('CMTrace', 'CMLogViewer', 'OneTrace')]
[String] $LogProgram = 'CMTrace',
[Parameter(Mandatory = $false, HelpMessage = "Specify the WindowStyle of CMTrace and File Explorer. Default value is 'normal'")]
[ValidateSet('Minimized', 'Maximized', 'Normal')]
[String] $LogProgramWindowStyle = 'Normal',
[Parameter(Mandatory = $false, HelpMessage = "If specified, the Tool won't open any history log files. Opening history log files is currently only supported with CMLogViewer.")]
[Switch] $DisableHistoryLogFiles,
[Parameter(Mandatory = $false, HelpMessage = 'Specify the number of recent log files which will be listed in the menu. Default value is 15')]
[Int] $RecentLogLimit = 15,
[Parameter(Mandatory = $false, HelpMessage = "If specified, the Tool won't prompt if there is a newer Version available")]
[Switch] $DisableUpdater,
[Parameter(Mandatory = $false, HelpMessage = 'If specified, the Tool will automatically open the corresponding logs when executing client actions.')]
[Switch] $EnableAutoLogLaunch
)
# General options
$toolVersion = '3.0.6'
$updateUrl = 'https://msitproblog.com/clfo_options.xml'
# Add Visual Basic Assembly for displaying message popups
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
# Create Shell Object, for handling CMTrace Inputs. (Usage of the .NET Classes led to CMTrace Freezes.)
$shellObj = New-Object -ComObject WScript.Shell
# Contains the information if the connected device is remote or local
$hostnameIsRemote = $true
# Get date time Pattern for Date/Time Conversion
$dateTimePattern = (Get-Culture).DateTimeFormat.ShortDatePattern + ' ' + (Get-Culture).DateTimeFormat.ShortTimePattern
$logfileTable = @{
'ccmsetup' = @{
'path' = 'C$\Windows\ccmsetup\Logs'
'logfiles' = @('ccmsetup.log')
}
'ccmupdate' = @{
'path' = $clientLogfilesDir
'logfiles' = @('ScanAgent.log', 'UpdatesDeployment.log', 'UpdatesHandler.log', 'UpdatesStore.log', 'WUAHandler.log')
}
'winupdate' = @{
'path' = 'C$\Windows'
'logfiles' = @('WindowsUpdate.log')
}
'ccmappdiscovery' = @{
'path' = $clientLogfilesDir
'logfiles' = @('AppDiscovery.log')
}
'ccmappenforce' = @{
'path' = $clientLogfilesDir
'logfiles' = @('AppEnforce.log')
}
'ccmexecmgr' = @{
'path' = $clientLogfilesDir
'logfiles' = @('execmgr.log')
}
'ccmexec' = @{
'path' = $clientLogfilesDir
'logfiles' = @('CcmExec.log')
}
'ccmstartup' = @{
'path' = $clientLogfilesDir
'logfiles' = @('ClientIDManagerStartup.log')
}
'ccmpolicy' = @{
'path' = $clientLogfilesDir
'logfiles' = @('PolicyAgent.log', 'PolicyAgentProvider.log', 'PolicyEvaluator.log', 'StatusAgent.log')
}
'ccmepagent' = @{
'path' = $clientLogfilesDir
'logfiles' = @('EndpointProtectionAgent.log')
}
'ccmdownload' = @{
'path' = $clientLogfilesDir
'logfiles' = @('CAS.log', 'CIDownloader.log', 'DataTransferService.log')
}
'ccmsetupeval' = @{
'path' = 'C$\Windows\ccmsetup\Logs'
'logfiles' = @('ccmsetup-ccmeval.log')
}
'ccminventory' = @{
'path' = $clientLogfilesDir
'logfiles' = @('InventoryAgent.log', 'InventoryProvider.log')
}
'ccmsmsts' = @{
'path' = $clientLogfilesDir
'logfiles' = @('smsts.log')
}
'ccmstatemessage' = @{
'path' = $clientLogfilesDir
'logfiles' = @('StateMessage.log')
}
'ccmscript' = @{
'path' = $clientLogfilesDir
'logfiles' = @('Scripts.log')
}
'winservicingsetupact' = @{
'path' = 'C$\Windows\Panther'
'logfiles' = @('setupact.log')
}
'winservicingsetuperr' = @{
'path' = 'C$\Windows\Panther'
'logfiles' = @('setuperr.log')
}
'scepmpcmdrun' = @{
'path' = 'C$\Windows\Temp'
'logfiles' = @('MpCmdRun.log')
}
}
$ccmBuildNoTable = @{
'7711' = '2012 RTM'
'7804' = '2012 SP1'
'8239' = '2012 SP2 / R2 SP1'
'7958' = '2012 R2 RTM'
'8325' = 'CB 1511'
'8355' = 'CB 1602'
'8412' = 'CB 1606'
'8458' = 'CB 1610'
'8498' = 'CB 1702'
'8540' = 'CB 1706'
'8577' = 'CB 1710'
'8634' = 'CB 1802'
'8692' = 'CB 1806'
'8740' = 'CB 1810'
'8790' = 'CB 1902'
'8853' = 'CB 1906'
'8913' = 'CB 1910'
'8968' = 'CB 2002'
'9012' = 'CB 2006'
'9040' = 'CB 2010'
'9049' = 'CB 2103'
'9058' = 'CB 2107'
'9068' = 'CB 2111'
'9078' = 'CB 2203'
'9088' = 'CB 2207'
'9096' = 'CB 2211'
'9106' = 'CB 2303'
}
$consoleExtensionXmlFile = 'ConfigMgr LogFile Opener.xml'
$consoleExtensionActionGUIDs = @('fb04b7a5-bc4c-4468-8eb8-937d8eb90efb', 'ed9dee86-eadd-4ac8-82a1-7234a4646e62', 'cbe3631f-901e-49ea-b3c2-4e32996720cd', '0770186d-ea57-4276-a46b-7344ae081b58', '64db983c-10bc-4b47-8f2d-cfff48f34faf', '3fd01cd1-9e01-461e-92cd-94866b8d1f39', '2b646eff-442b-410e-adf3-d4ec699e0ab4')
$consoleExtensionXmlContent = '<ActionDescription Class="Executable" DisplayName="Start ConfigMgr LogFile Opener" MnemonicDisplayName="Start ConfigMgr LogFile Opener" Description = "Start ConfigMgr LogFile Opener">
<ShowOn>
<string>ContextMenu</string>
</ShowOn>
<ImagesDescription>
<ResourceAssembly>
<Assembly>AdminUI.UIResources.dll</Assembly>
<Type>Microsoft.ConfigurationManagement.AdminConsole.UIResources.Properties.Resources.resources</Type>
</ResourceAssembly>
<ImageResourceName>Tool</ImageResourceName>
</ImagesDescription>
<Executable>
<FilePath>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</FilePath>
<Parameters>-ExecutionPolicy Bypass -File "' + $MyInvocation.MyCommand.Path + '" -Hostname ##SUB:Name##</Parameters>
</Executable>
</ActionDescription>'
Function Open-LogFile ([String] $Action) {
# Get action from Hash Table, and throw error if it does not exist
$actionHandler = $logfileTable.GetEnumerator() | Where-Object { $_.Key -eq $action }
If (!$actionHandler) {
Invoke-MessageBox -Message "Action '$action' can not be found in Hash Table"
Return
}
# Assign values from Hash Table
$logfilePath = "\\$hostname\$($actionHandler.Value.path)"
$logfiles = $actionHandler.Value.logfiles
# Check if logfile path is accessible
If (!(Test-Path -Path $logfilePath)) {
Invoke-MessageBox -Message "'$logfilePath' is not accessible!"
Return
}
Invoke-LogProgram -Path $logfilePath -Files $logfiles
}
Function Invoke-CMTrace ([String] $Path, [Array] $Files) {
# Check if CMTrace exists
If (!(Test-Path -Path $cmtrace)) {
Invoke-MessageBox -Message "'$cmtrace' is not accessible!"
Return
}
# Check if CMTrace was started at least once. This is needed to make sure that the initial FTA PopUp doesn't appear.
If (!(Test-Path -Path 'HKCU:\Software\Microsoft\Trace32')) {
Invoke-MessageBox -Message "CMTrace needs be started at least once. Click 'OK' to launch CMTrace, confirm all dialogs and try again." -Icon 'Exclamation'
# Empty files array to start a single CMTrace Instance
$files = @()
}
# Write current path in Registry
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Trace32' -Value $path -Name 'Last Directory' -Force
# Check if multiple files were specified
If ($files.Count -gt 1) {
# Start CMTrace and wait until it's open
Start-Process -FilePath $cmtrace
Start-Sleep -Milliseconds $actionDelayShort
# Send CTRL+O to open the open file dialog
$shellObj.SendKeys('^o')
Start-Sleep -Milliseconds $actionDelayShort
# Write logfiles name in CMTrace format, "Log1" "Log2" "Log3" etc.
$shellObj.SendKeys('"' + [String]::Join('" "', $files) + '"')
# Navigate to Merge checkbox and enable it
$shellObj.SendKeys('{TAB}{TAB}{TAB}{TAB}{TAB}')
$shellObj.SendKeys(' ')
# Send ENTER
$shellObj.SendKeys('{ENTER}')
# Wait until log file is loaded
Start-Sleep -Milliseconds $actionDelayLong
# Send CTRL + END to scroll to the bottom
$shellObj.SendKeys('^{END}')
# Set Empty path in registry
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Trace32' -Value '' -Name 'Last Directory' -Force
}
# Check if one file was specified
ElseIf ($files.Count -eq 1) {
# Build full logfile path
$fullLogfilePath = $path + '\' + [String]::Join(' ', $files)
# Check if Logfile exists
If (!(Test-Path -Path $fullLogfilePath)) {
Invoke-MessageBox -Message "'$fullLogfilePath' is not accessible!"
Return
}
# Open Logfile in CMTrace
Start-Process -FilePath $cmtrace -ArgumentList $fullLogfilePath
# Wait until log file is loaded
Start-Sleep -Milliseconds $actionDelayShort
# Send CTRL + END to scroll to the bottom
$shellObj.SendKeys('^{END}')
# Set Empty path in registry
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Trace32' -Value '' -Name 'Last Directory' -Force
}
# Check if no file was specified
Else {
# Open CMTrace
Start-Process -FilePath $cmtrace
}
# Check WindowStyle. NOTE: CMTrace can't be launched using the native 'WindowStyle' Attribute via Start-Process above.
Switch ($logProgramWindowStyle) {
'Minimized' {
$shellObj.SendKeys('% n')
}
'Maximized' {
$shellObj.SendKeys('% x')
}
}
}
Function Invoke-CMLogViewer ([String] $Path, [Array] $Files) {
# Check if CMLogViewer exists
If (!(Test-Path -Path $cmLogViewer)) {
Invoke-MessageBox -Message "'$cmLogViewer' is not accessible! Please install the 'Configuration Manager Support Center' from the tools folder."
Return
}
# Check if log files were specified
If ($Files -gt 1) {
# Check if History Logfiles are disabled
If (!$disableHistoryLogFiles) {
$discoveredFiles = @()
# Go through each log file
foreach ($file in $files) {
# Search for history log files
Get-ChildItem -Path $path -Filter ('*' + $file.TrimEnd('.log') + '*') | ForEach-Object {
$discoveredFiles += $_.Name
}
}
# assign new log files array
$files = $discoveredFiles
}
# Build full logfile path: "Path\Log1" "Path\Log2" "Path\Log3" etc.
foreach ($file in $files) {
$fullLogfilePath += '"' + $Path + '\' + $file + '" '
}
# Open Logfile in CMLogViewer
Start-Process -FilePath $cmLogViewer -ArgumentList $fullLogfilePath -WindowStyle $logProgramWindowStyle
}
# Check if no files were specified
Else {
# Open CMLogViewer
Start-Process -FilePath $cmLogViewer -WindowStyle $logProgramWindowStyle
}
}
Function Invoke-OneTrace ([String] $Path, [Array] $Files) {
# Check if OneTrace exists
If (!(Test-Path -Path $oneTrace)) {
Invoke-MessageBox -Message "'$oneTrace' is not accessible! Please install the 'Configuration Manager Support Center' from the tools folder."
Return
}
# Check if log files were specified
If ($Files -gt 1) {
# Start OneTrace and wait until it's open
Start-Process -FilePath $oneTrace
Start-Sleep -Milliseconds $actionDelayLong
# Send ALT to select the menu bar
$shellObj.SendKeys('%')
Start-Sleep -Milliseconds 500
# Send F to select the File Dialog
$shellObj.SendKeys('F')
Start-Sleep -Milliseconds 500
# Send Enter to open the current selection
$shellObj.SendKeys('{ENTER}')
Start-Sleep -Milliseconds $actionDelayShort
# Write path into open dialog
$shellObj.SendKeys($path)
Start-Sleep -Milliseconds $actionDelayShort
# Send Enter to switch to the specified path
$shellObj.SendKeys('{ENTER}')
Start-Sleep -Milliseconds $actionDelayShort
# Write logfiles name in OneTrace format, "Log1" "Log2" "Log3" etc.
$shellObj.SendKeys('"' + [String]::Join('" "', $files) + '"')
# Send ENTER
$shellObj.SendKeys('{ENTER}')
}
# Check if no files were specified
Else {
# Open OneTrace
Start-Process -FilePath $oneTrace
}
# Check WindowStyle. NOTE: OneTrace can't be launched using the native 'WindowStyle' Attribute via Start-Process above.
Switch ($logProgramWindowStyle) {
'Minimized' {
$shellObj.SendKeys('% n')
}
'Maximized' {
$shellObj.SendKeys('% x')
}
}
}
Function Invoke-LogProgram([String] $Path, [Array] $Files) {
If ($logProgram -eq 'CMTrace') {
Invoke-CMTrace -Path $path -Files $files
}
ElseIf ($logProgram -eq 'CMLogViewer') {
Invoke-CMLogViewer -Path $path -Files $files
}
ElseIf ($logProgram -eq 'OneTrace') {
Invoke-OneTrace -Path $path -Files $files
}
}
Function Open-Path ([String] $Path) {
# build full path
$logfilePath = "\\$hostname\$Path"
# Check if path is accessible
If (!(Test-Path -Path $logfilePath)) {
Invoke-MessageBox -Message "'$logfilePath' is not accessible!"
}
Else {
# Open File explorer
Start-Process -FilePath 'C:\Windows\explorer.exe' -ArgumentList $logfilePath -WindowStyle $logProgramWindowStyle
}
}
Function Invoke-ClientAction([String[]] $Action, [String] $LogFile, [bool] $ActionOnly = $false) {
Try {
# Set ErrorActionPreference to stop, otherwise Try/Catch won't have an effect on Invoke-WmiMethod
$ErrorActionPreference = 'Stop'
foreach ($singleAction in $action) {
# Trigger specified WMI Method on Client. Note: Invoke-Cim Command doesn't work here --> Error 0x8004101e
# <https://powershell.org/forums/topic/invoke-cimmethod-executes-correct-but-returns-wmi-error-0x8004101e/>
If ($hostnameIsRemote) {
Invoke-WmiMethod -ComputerName $hostname -Namespace 'root\CCM' -Class 'SMS_Client' -Name 'TriggerSchedule' -ArgumentList ('{' + $singleAction + '}') | Out-Null
}
Else {
Invoke-WmiMethod -Namespace 'root\CCM' -Class 'SMS_Client' -Name 'TriggerSchedule' -ArgumentList ('{' + $singleAction + '}') | Out-Null
}
}
If ($actionOnly -eq $false) {
# Display message box
Invoke-MessageBox -Message 'The Client Action has been executed' -Icon 'Information'
# Open corresponding log file
If ($enableAutoLogLaunch -and $logFile -ne '') {
Open-LogFile -Action $LogFile
}
}
}
Catch {
# Display error message in case of a failure and return to the client action menu
$errorMessage = $_.Exception.Message
Invoke-MessageBox -Message "Unable to execute the specified Client Action.`n`n$errorMessage"
}
}
Function Invoke-MessageBox([String] $Message, [String] $Icon = 'Critical', [String] $Button = 'OKOnly') {
Return [Microsoft.VisualBasic.Interaction]::MsgBox($message, "$button,MsgBoxSetForeground,$icon", 'ConfigMgr LogFile Opener')
}
Function Get-ClientVersionString {
Try {
# Get client version from WMI
If ($hostnameIsRemote) {
$clientVersion = Get-CimInstance -ComputerName $hostname -Namespace 'root\CCM' -ClassName 'SMS_Client' -Property 'ClientVersion' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'ClientVersion'
}
Else {
$clientVersion = Get-CimInstance -Namespace 'root\CCM' -ClassName 'SMS_Client' -Property 'ClientVersion' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'ClientVersion'
}
# Extract build number from client version
$ccmBuildNo = $clientVersion.Split('.')[2]
# Get BuildNo String from hash table
$ccmBuildNoHandler = $ccmBuildNoTable.GetEnumerator() | Where-Object { $_.Key -eq $ccmBuildNo }
# Build client version string
If ($ccmBuildNoHandler) {
$clientVersionString = "$($ccmBuildNoHandler.Value) ($clientVersion)"
}
Else {
$clientVersionString = $clientVersion
}
Return $clientVersionString
}
Catch {
Return 'n/a'
}
}
Function Get-OperatingSystemData {
$data = @{}
[uint32]$ubrKey = 2147483650 # HKEY_LOCAL_MACHINE
$ubrSubKeyName = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion'
$ubrValueName = 'UBR'
Try {
# Get operating system data from WMI
If ($hostnameIsRemote) {
$osCimObject = Get-CimInstance -ComputerName $hostname -ClassName 'Win32_OperatingSystem' -Property Caption, Version, OSArchitecture, LastBootUpTime -ErrorAction SilentlyContinue
$sysCimObject = Get-CimInstance -ComputerName $hostname -ClassName 'Win32_ComputerSystem' -Property Domain -ErrorAction SilentlyContinue
$ubrCimObject = Invoke-CimMethod -ComputerName $hostname -Namespace 'root\default' -ClassName StdRegProv -MethodName GetDwordValue -Arguments @{hDefKey = $ubrKey; sSubKeyName = $ubrSubKeyName; sValueName = $ubrValueName } -ErrorAction SilentlyContinue
}
Else {
$osCimObject = Get-CimInstance -ClassName 'Win32_OperatingSystem' -Property Caption, Version, OSArchitecture, LastBootUpTime -ErrorAction SilentlyContinue
$sysCimObject = Get-CimInstance -ClassName 'Win32_ComputerSystem' -Property Domain -ErrorAction SilentlyContinue
$ubrCimObject = Invoke-CimMethod -Namespace 'root\default' -ClassName StdRegProv -MethodName GetDwordValue -Arguments @{hDefKey = $ubrKey; sSubKeyName = $ubrSubKeyName; sValueName = $ubrValueName } -ErrorAction SilentlyContinue
}
# Remove unneeded things from OS caption
$data.osString = "$($osCimObject.Caption.Replace('Microsoft', '').Trim())"
$data.osString = $data.osString.Replace('Enterprise', 'Ent.')
$data.osString = $data.osString.Replace('Standard', 'Std.')
# Add Architecture if this is a non Server Operating System
If ($data.osString -notmatch 'Windows Server*') {
$data.osString = $data.osString + " $($osCimObject.OSArchitecture)"
}
# Add BuildNo
$data.osString = $data.osString + " ($($osCimObject.Version).$($ubrCimObject.uValue))"
$data.lastBootTime = Get-Date $osCimObject.LastBootUpTime -Format $dateTimePattern
$data.domain = $sysCimObject.Domain
Return $data
}
Catch {
$data.osString = 'n/a'
$data.lastBootTime = 'n/a'
$data.domain = 'n/a'
Return $data
}
}
Function Get-ModelString {
Try {
# Get client version from WMI
If ($hostnameIsRemote) {
$cimObject = Get-CimInstance -ComputerName $hostname -ClassName 'Win32_ComputerSystemProduct' -Property Vendor, Version -ErrorAction SilentlyContinue
}
Else {
$cimObject = Get-CimInstance -ClassName 'Win32_ComputerSystemProduct' -Property Vendor, Version -ErrorAction SilentlyContinue
}
If ($cimObject.Vendor -eq 'HP') {
# Special Handling for HP Devices
If ($hostnameIsRemote) {
$cimObject2 = Get-CimInstance -ComputerName $hostname -ClassName 'Win32_ComputerSystem' -Property Model -ErrorAction SilentlyContinue
}
Else {
$cimObject2 = Get-CimInstance -ClassName 'Win32_ComputerSystem' -Property Model -ErrorAction SilentlyContinue
}
Return "$($cimObject.Vendor) $($cimObject2.Model)"
}
Else {
Return "$($cimObject.Vendor) $($cimObject.Version)"
}
}
Catch {
Return 'n/a'
}
}
Function Get-RecentLog {
$logfilePath = "\\$hostname\$clientLogFilesDir"
# Check if CCM Logfile path is accessible
If (!(Test-Path -Path $logfilePath)) {
Invoke-MessageBox -Message "Unable to access '$logfilePath'." | Out-Null
Return $false
}
# Check if CCM Logfile path contains any logs
If (!(Get-ChildItem $logfilePath).Count) {
Invoke-MessageBox -Message "Log directory '$logfilePath' doesn't contain any Log files." -Icon 'Exclamation' | Out-Null
Return $false
}
# Get Recent Log Files
$logs = Get-ChildItem $logfilePath | Sort-Object LastWriteTime -Descending | Select-Object Name, LastWriteTime -First $RecentLogLimit
$list = @{}
$listIndex = 1
foreach ($log in $logs) {
# Add Log data into hash table
$list[$listIndex] += @{
'Name' = $log.Name
'Path' = $logfilePath
'LastWriteTime' = Get-Date $log.LastWriteTime -Format $dateTimePattern
}
$listIndex++
}
# Return sorted Hash Table
Return $list.GetEnumerator() | Sort-Object -Property Name
}
Function Test-ConsoleInstallation {
If (Test-Path $env:SMS_ADMIN_UI_PATH) {
Return $true
}
Else {
Return $false
}
}
Function Test-Elevation {
Return (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
Function Install-ConsoleExtension {
If (!(Test-ConsoleInstallation)) {
Invoke-MessageBox -Message 'No ConfigMgr Console found on this System.'
Return
}
If (!(Test-Elevation)) {
Invoke-MessageBox -Message 'Please run ConfigMgr LogFile Opener as an Administrator to install the Console Extension.'
Return
}
foreach ($guid In $consoleExtensionActionGUIDs) {
# Build path from GUID
$path = "$($env:SMS_ADMIN_UI_PATH)..\..\..\XmlStorage\Extensions\Actions\$guid"
# Create Actions Folder if needed
If (!(Test-Path $path)) {
New-Item -ItemType Directory -Path $path | Out-Null
}
# Populate Extension XML into Actions Path
$consoleExtensionXmlContent | Out-File "$path\$consoleExtensionXmlFile" -Force -Encoding UTF8
}
Invoke-MessageBox -Message 'Console Extension successfully installed/updated. Please restart all open ConfigMgr Consoles.' -Icon Information
}
Function Remove-ConsoleExtension {
If (!(Test-Elevation)) {
Invoke-MessageBox -Message 'Please run this tool as an Administrator to remove the Console Extension.'
Return
}
foreach ($guid In $consoleExtensionActionGUIDs) {
# Build file path from GUID
$file = "$($env:SMS_ADMIN_UI_PATH)..\..\..\XmlStorage\Extensions\Actions\$guid\$consoleExtensionXmlFile"
# Remove Extension XML if exists
If (Test-Path $file) {
Remove-Item $file -Force
}
}
Invoke-MessageBox -Message 'Console Extension successfully removed. Please restart all open ConfigMgr Consoles.' -Icon Information
}
Function Invoke-ToolUpdater {
# Get XML Document Object
$xml = New-Object System.Xml.XmlDocument
# Use TLS 1.2 for a Secure Connection to the Update XML. Systems with older .NET Framework Versions use SSL3, TLS which will fail
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
# Try to load updater options
Try {
$xml.Load($updateUrl)
$currentVersion = $xml.options.currentVersion
$downloadPage = $xml.options.downloadPage.'#cdata-section'
}
Catch {
Return $false
}
If ([System.Version] $toolVersion -lt [System.Version] $currentVersion) {
$response = [Microsoft.VisualBasic.Interaction]::MsgBox("Version $currentVersion of ConfigMgr LogFile Opener is available. Do you want to Download the latest version?", 'YesNo,MsgBoxSetForeground,Information', "ConfigMgr LogFile Opener - $toolVersion")
If ($response -eq 'Yes') {
Start-Process $downloadPage
}
}
}
Function Stop-CcmExec {
If ((Invoke-MessageBox -Message "Do you really want to stop the ConfigMgr Client service on $($hostname)?" -Icon Information -Button YesNo) -eq 'Yes') {
Try {
If ($hostnameIsRemote) {
$serviceObject = Get-Service -Name CcmExec -ComputerName $hostname | Stop-Service -PassThru
}
Else {
$serviceObject = Get-Service -Name CcmExec | Stop-Service -PassThru
}
If ($serviceObject.Status -eq 'Stopped') {
Invoke-MessageBox -Message "ConfigMgr Client service successfully stopped on $($hostname)." -Icon Information
# Open corresponding log file
If ($enableAutoLogLaunch) {
Open-LogFile -Action 'ccmexec'
}
}
Else {
Invoke-MessageBox -Message "Unable to stop the ConfigMgr Client service on $($hostname).`n`nCurrent service status: $($service.Status)"
}
}
Catch {
# Display error message in case of a failure and return to the client action menu
$errorMessage = $_.Exception.Message
Invoke-MessageBox -Message "Unable to stop the ConfigMgr Client service on $($hostname).`n`n$errorMessage"
}
}
}
Function Start-CcmExec {
$ErrorActionPreference = 'Stop'
Try {
If ($hostnameIsRemote) {
$serviceObject = Get-Service -Name CcmExec -ComputerName $hostname | Start-Service -PassThru
}
Else {
$serviceObject = Get-Service -Name CcmExec | Start-Service -PassThru
}
If ($serviceObject.Status -eq 'Running') {
Invoke-MessageBox -Message "ConfigMgr Client service successfully started on $($hostname)." -Icon Information
# Open corresponding log file
If ($enableAutoLogLaunch) {
Open-LogFile -Action 'ccmexec'
}
}
Else {
Invoke-MessageBox -Message "Unable to start the ConfigMgr Client service on $($hostname).`n`nCurrent service status: $($service.Status)"
}
}
Catch {
# Display error message in case of a failure and return to the client action menu
$errorMessage = $_.Exception.Message
Invoke-MessageBox -Message "Unable to start the ConfigMgr Client service on $($hostname).`n`n$errorMessage"
}
}
Function Restart-CcmExec {
$ErrorActionPreference = 'Stop'
If ((Invoke-MessageBox -Message "Do you really want to restart the ConfigMgr Client service on $($hostname)?" -Icon Information -Button YesNo) -eq 'Yes') {
Try {
If ($hostnameIsRemote) {
$serviceObject = Get-Service -Name CcmExec -ComputerName $hostname | Restart-Service -PassThru
}
Else {
$serviceObject = Get-Service -Name CcmExec | Restart-Service -PassThru
}
If ($serviceObject.Status -eq 'Running') {
# Open corresponding log file
If ($enableAutoLogLaunch) {
Open-LogFile -Action 'ccmexec'
}
Invoke-MessageBox -Message "ConfigMgr Client service successfully restarted on $($hostname)." -Icon Information
}
Else {
Invoke-MessageBox -Message "Unable to restart the ConfigMgr Client service on $($hostname).`n`nCurrent service status: $($service.Status)"
}
}
Catch {
# Display error message in case of a failure and return to the client action menu
$errorMessage = $_.Exception.Message
Invoke-MessageBox -Message "Unable to restart the ConfigMgr Client service on $($hostname).`n`n$errorMessage"
}
}
}
Function Invoke-ConfigurationBaselineEvaluation {
$ErrorActionPreference = 'Stop'
Try {
$baselineCount = 0
If ($hostnameIsRemote) {
Get-CimInstance -ComputerName $hostname -ClassName 'SMS_DesiredConfiguration' -Namespace 'root\ccm\dcm' | ForEach-Object {
([wmiclass]"\\$hostname\root\ccm\dcm:SMS_DesiredConfiguration").TriggerEvaluation($_.Name, $_.Version) | Out-Null
$baselineCount++
}
}
Else {
Get-CimInstance -ClassName 'SMS_DesiredConfiguration' -Namespace 'root\ccm\dcm' | ForEach-Object {
([wmiclass]'root\ccm\dcm:SMS_DesiredConfiguration').TriggerEvaluation($_.Name, $_.Version) | Out-Null
$baselineCount++
}
}
If ($baselineCount -eq 1) {
$messageText = "$baselineCount Configuration Baseline has been reevaluated on $($hostname)."
}
Else {
$messageText = "$baselineCount Configuration Baselines have been reevaluated on $($hostname)."
}
Invoke-MessageBox -Message $messageText -Icon Information
}
Catch {
# Display error message in case of a failure and return to the client action menu
$errorMessage = $_.Exception.Message
Invoke-MessageBox -Message "Unable to reevaluate Configuration Baselines on $($hostname).`n`n$errorMessage"
}
}
Function Invoke-CcmEval {
$ErrorActionPreference = 'Stop'
Try {
If ($hostnameIsRemote) {
# Create PowerShell Session for target computer
$cimSession = New-CimSession -ComputerName $hostname
# Run ccmeeval Task on target computer
Start-ScheduledTask -CimSession $cimSession -TaskPath '\Microsoft\Configuration Manager' -TaskName 'Configuration Manager Health Evaluation'
# Terminate PowerShell Session
Remove-CimSession -CimSession $cimSession
}
Else {
Start-ScheduledTask -TaskPath '\Microsoft\Configuration Manager' -TaskName 'Configuration Manager Health Evaluation'
}
Invoke-MessageBox -Message "ConfigMgr Client Evaluation has been executed on $($hostname)." -Icon Information
# Open corresponding log file
If ($enableAutoLogLaunch) {
Open-LogFile -Action 'ccmsetupeval'
}
}
Catch {
# Display error message in case of a failure and return to the client action menu
$errorMessage = $_.Exception.Message
Invoke-MessageBox -Message "Unable to run ConfigMgr Client Health Evaluation on $($hostname).`n`n$errorMessage"
Return $false
}
}
Function Update-SoftwareUpdateComplianceState {
$ErrorActionPreference = 'Stop'
Try {
If ($hostnameIsRemote) {
# Create PowerShell Session for target computer
$psSession = New-PSSession -ComputerName $hostname
# Refresh Software Update Compliance State
Invoke-Command -Session $psSession -ScriptBlock {
$updatesStore = New-Object -ComObject Microsoft.CCM.UpdatesStore
$updatesStore.RefreshServerComplianceState()
} | Out-Null
# Terminate PowerShell Session
Remove-PSSession -Session $psSession
}
Else {
$updatesStore = New-Object -ComObject Microsoft.CCM.UpdatesStore
$updatesStore.RefreshServerComplianceState()
}
Invoke-MessageBox -Message "Software Update Compliance State has been successfully refreshed on $($hostname)." -Icon Information
# Open corresponding log file
If ($enableAutoLogLaunch) {
Open-LogFile -Action 'ccmupdate'
}
}
Catch {
# Display error message in case of a failure and return to the client action menu
$errorMessage = $_.Exception.Message
Invoke-MessageBox -Message "Unable to run refresh Software Update Compliance State on $($hostname).`n`n$errorMessage"
Return $false
}
}
Function Get-IPAddressString {
$ErrorActionPreference = 'Stop'
# Check if hostname is an ip address
Try {
If ($hostname -match '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b') {
# If an IP address was used for the device connection then don't perform a dns lookup
$ipAddress = $hostname
}
Else {
# Determine Client IP address via DNS
# TODO: This is currently limited to IPv4 and a single ip address
$ipAddress = Resolve-DnsName $hostname | Where-Object { $_.Type -eq 'A' } | Select-Object -ExpandProperty IPAddress -First 1
}
# Determine adapter type
If ($hostnameIsRemote) {
$netAdapterName = Get-CimInstance -ComputerName $hostname -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -eq $ipAddress } -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Description
$adapterTypeID = Get-CimInstance -ComputerName $hostname -Namespace 'root/WMI' -Class MSNdis_PhysicalMediumType -ErrorAction SilentlyContinue | Where-Object { $_.InstanceName -eq $netAdapterName } | Select-Object -ExpandProperty NdisPhysicalMediumType
}
Else {
$netAdapterName = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -eq $ipAddress } -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Description
$adapterTypeID = Get-CimInstance -Namespace 'root/WMI' -Class MSNdis_PhysicalMediumType -ErrorAction SilentlyContinue | Where-Object { $_.InstanceName -eq $netAdapterName } | Select-Object -ExpandProperty NdisPhysicalMediumType
}
$adapterType = ''
Switch ($adapterTypeID) {
0 {
$adapterType = 'LAN'
}
9 {
$adapterType = 'WLAN'
}
8 {
$adapterType = 'WWAN'
}
}
If ($adapterType -eq '') {
Return "$ipAddress"
}
Else {
Return "$ipAddress ($adapterType)"
}
}
Catch {
return 'n/a'
}
}
Function Invoke-CcmRepair {
$ErrorActionPreference = 'Stop'
If ((Invoke-MessageBox -Message "Do you really want to repair the ConfigMgr Client on $($hostname)?" -Icon Information -Button YesNo) -eq 'Yes') {
Try {
# Connect to WMI
If ($hostnameIsRemote) {
$wmi = [wmiclass] "\\$hostname\root\ccm:sms_client"
}
Else {
$wmi = [wmiclass] '\root\ccm:sms_client'
}
# Trigger Client Repair
If ($wmi.RepairClient()) {
Invoke-MessageBox -Message "ConfigMgr Client Repair has been successfully started on $($hostname)." -Icon Information
# Open corresponding log file
If ($enableAutoLogLaunch) {
Open-LogFile -Action 'ccmsetup'
}
}
Else {
Invoke-MessageBox -Message "Unable to start the ConfigMgr Client Repair on $($hostname)."
}
}