This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Microsoft.WindowsAdminCenter.Configuration.psm1
3930 lines (3305 loc) · 148 KB
/
Microsoft.WindowsAdminCenter.Configuration.psm1
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
Set-Variable -Name ConstNetShCommand -Option Constant -Value "netsh.exe"
Set-Variable -Name ConstWevutilCommand -Option Constant -Value "wevtutil.exe"
Set-Variable -Name ConstServiceController -Option Constant -Value "sc.exe"
Set-Variable -Name ConstServiceName -Option Constant -Value "WindowsAdminCenter"
Set-Variable -Name ConstAccountManagementServiceName -Option Constant -Value "WindowsAdminCenterAccountManagement"
Set-Variable -Name ConstUpdaterScheduledTaskName -Option Constant -Value "WindowsAdminCenterUpdater"
Set-Variable -Name ConstLauncherName -Option Constant -Value "WindowsAdminCenterLauncher"
Set-Variable -Name ConstEventLogName -Option Constant -Value "WindowsAdminCenter"
Set-Variable -Name ConstDisplayName -Option Constant -Value "Windows Admin Center"
Set-Variable -Name ConstAccountManagementServiceDisplayName -Option Constant -Value "Windows Admin Center Account Management"
Set-Variable -Name ConstUpdaterServiceDisplayName -Option Constant -Value "Windows Admin Center Updater"
Set-Variable -Name ConstServiceDescription -Option Constant -Value "Manage remote Windows computers from web service."
Set-Variable -Name ConstAccountManagementServiceDescription -Option Constant -Value "Manage AAD token and account for Windows Admin Center."
Set-Variable -Name ConstUpdaterServiceDescription -Option Constant -Value "Install updates for Windows Admin Center."
Set-Variable -Name ConstExecutableName -Option Constant -Value "WindowsAdminCenter.exe"
Set-Variable -Name ConstLauncherExecutableName -Option Constant -Value "WindowsAdminCenterLauncher.exe"
Set-Variable -Name ConstAccoutManagementExecutableName -Option Constant -Value "WindowsAdminCenterAccountManagement.exe"
Set-Variable -Name ConstUpdaterExecutableName -Option Constant -Value "WindowsAdminCenterUpdater.exe"
Set-Variable -Name ConstAppConfigJsonName -Option Constant -Value "appsettings.json"
Set-Variable -Name ConstTokenAuthenticationModeTag -Option Constant -Value """TokenAuthenticationMode"":"
Set-Variable -Name ConstSubjectTag -Option Constant -Value """Subject"":"
Set-Variable -Name ConstProgramDataFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter"
Set-Variable -Name ConstServiceFolderPath -Option Constant -Value "${env:ProgramFiles}\WindowsAdminCenter\Service"
Set-Variable -Name ConstUpdaterFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\Updater"
Set-Variable -Name ConstAppId -Option Constant -Value "{13EF9EED-B613-4D2D-8B82-7E5B90BE4990}"
Set-Variable -Name ConstDefaultPort -Option Constant -Value "6600"
Set-Variable -Name ConstUsersSecurityDescriptor -Option Constant -Value "D:(A;;GX;;;AU)(A;;GX;;;NS)"
Set-Variable -Name ConstNetworkServiceSecurityDescriptor -Option Constant -Value "D:(A;;GX;;;NS)"
Set-Variable -Name ConstCertificateKeySecurityDescriptor -Option Constant -Value "O:SYG:SYD:AI(A;;GAGR;;;SY)(A;;GAGR;;;NS)(A;;GAGR;;;BA)(A;;GR;;;BU)"
Set-Variable -Name ConstAccountManagementSecurityDescriptor -Option Constant -Value "D:(A;;CCLCSWRPWPLO;;;NS)(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)"
Set-Variable -Name ConstUpdaterScheduledTaskSecurityDescriptor -Option Constant -Value "O:BAD:AI(A;;FR;;;SY)(A;;0x1200a9;;;NS)(A;ID;0x1f019f;;;BA)(A;ID;0x1f019f;;;SY)(A;ID;FA;;;BA)"
Set-Variable -Name ConstSSLCertificateSubjectName -Option Constant -Value "WindowsAdminCenterSelfSigned"
Set-Variable -Name ConstTestSSLCertificateSubjectName -Option Constant -Value "WindowsAdminCenterTestSelfSigned"
Set-Variable -Name ConstSSLCertificateSubjectCN -Option Constant -Value "CN=WindowsAdminCenterSelfSigned"
Set-Variable -Name ConstTestSSLCertificateSubjectCN -Option Constant -Value "CN=WindowsAdminCenterTestSelfSigned"
Set-Variable -Name ConstRootCACertificateSubjectName -Option Constant -Value "WindowsAdminCenterSelfSignedRootCA"
Set-Variable -Name ConstTestRootCACertificateSubjectName -Option Constant -Value "WindowsAdminCenterTestSelfSignedRootCA"
Set-Variable -Name ConstRootCACertificateSubjectCN -Option Constant -Value "CN=WindowsAdminCenterSelfSignedRootCA"
Set-Variable -Name ConstTestRootCACertificateSubjectCN -Option Constant -Value "CN=WindowsAdminCenterTestSelfSignedRootCA"
Set-Variable -Name ConstInboundOpenException -Option Constant -Value "WacInboundOpenException";
Set-Variable -Name ConstCredSspName -Option Constant -Value "Microsoft.WindowsAdminCenter.Credssp"
Set-Variable -Name ConstCredSspGroupName -Option Constant -Value "Windows Admin Center CredSSP"
Set-Variable -Name ConstCredSspGroupDescription -Option Constant -Value "Members of CredSSP operations"
Set-Variable -Name ConstCredSspRoleName -Option Constant -Value "MS-CredSSP-Admin"
Set-Variable -Name ConstShellModuleName -Option Constant -Value "Microsoft.SME.Shell"
Set-Variable -Name ConstRoleCapabilitiesName -Option Constant -Value "Microsoft.WindowsAdminCenter.CredSspPolicy"
Set-Variable -Name ConstCredSspAdmin -Option Constant -Value "MS-CredSSP-Admin"
Set-Variable -Name ConstPolicyFolderPath -Option Constant -Value "${env:ProgramFiles}\WindowsPowerShell\Modules\Microsoft.WindowsAdminCenter.CredSspPolicy"
Set-Variable -Name ConstShellModuleFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\Ux\powershell-module"
Set-Variable -Name ConstCredSspFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\CredSSP"
Set-Variable -Name ConstExtensionsFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\Extensions"
Set-Variable -Name ConstPluginsFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\Plugins"
Set-Variable -Name ConstUxFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\Ux"
Set-Variable -Name ConstModulesFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\Ux\modules"
Set-Variable -Name ConstExtensionsConfigFileName -Option Constant -Value "extensions.config"
Set-Variable -Name ConstExtensionManifestFileName -Option Constant -Value "manifest.json"
Set-Variable -Name ConstExtensionSettingsFileName -Option Constant -Value "settings.json"
Set-Variable -Name ConstExtensionUxFolderName -Option Constant -Value "Ux"
Set-Variable -Name ConstExtensionGatewayFolderName -Option Constant -Value "gateway"
Set-Variable -Name ConstExtensionCatalogsFolderName -Option Constant -Value "Catalogs"
Set-Variable -Name ConstExtensionPackagesFolderName -Option Constant -Value "Packages"
Set-Variable -Name ConstExtensionIndexFileName -Option Constant -Value "index.html"
Set-Variable -Name ConstRoleCapabilities -Option Constant -Value "RoleCapabilities"
Set-Variable -Name ConstWinRmCommand -Option Constant -Value "winrm.cmd"
Set-Variable -Name ConstLogFolderPath -Option Constant -Value "${env:ProgramData}\WindowsAdminCenter\Logs"
Set-Variable -Name ConstLogFileName -Option Constant -Value "Configuration.log"
Set-Variable -Name ConstEntityFrameworkBundleFileName -Option Constant -Value "efbundle.exe"
Set-Variable -Name ConstCoreDllFileName -Option Constant -Value "Microsoft.WindowsAdminCenter.Core.dll"
Set-Variable -Name ConstMachineKeyRootPath -Option Constant -Value "${env:ProgramData}\Microsoft\Crypto\RSA\MachineKeys"
Set-Variable -Name ConstSystemObject -Option Constant -Value "SYSTEM"
Set-Variable -Name ConstNetworkServiceSid -Option Constant -Value "S-1-5-20"
Set-Variable -Name ConstFullControlPermissions -Option Constant -Value "FullControl"
Set-Variable -Name ConstNuGetVersioningDllName -Option Constant -Value "NuGet.Versioning.dll"
#Requires -RunAsAdministrator
enum ExtensionStatus {
None = 0
Available = 1
Installed = 2
InstallPending = 3
UnInstallPending = 4
UpdatePending = 5
}
<#
.SYNOPSIS
Imports the build signer certificate.
.DESCRIPTION
Imports the build signer certificate to the TrustedPublisher store.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Import-WACBuildSignerCertificate
#>
function Import-WACBuildSignerCertificate {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$moduleFiles = Get-ChildItem -Path "$PSScriptRoot\..\.." -Include @('*.psm1', '*.psd1') -Recurse
$importedThumbprints = @{}
foreach ($moduleFile in $moduleFiles) {
$moduleAuthenticodeSignature = Get-AuthenticodeSignature -FilePath $moduleFile.FullName
if ($moduleAuthenticodeSignature.Status -ne "Valid") {
continue
}
if (-not $importedThumbprints.Contains($moduleAuthenticodeSignature.SignerCertificate.Thumbprint) -and
-not (Test-Path -Path (Join-Path -Path 'Cert:\LocalMachine\TrustedPublisher' -ChildPath $moduleAuthenticodeSignature.SignerCertificate.Thumbprint))) {
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store -ArgumentList ([System.Security.Cryptography.X509Certificates.StoreName]::TrustedPublisher),
([System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite -bor [System.Security.Cryptography.X509Certificates.OpenFlags]::MaxAllowed)
$store.Add($moduleAuthenticodeSignature.SignerCertificate)
$store.Close()
}
}
if ($importedThumbprints.Count -gt 0) {
Write-Log -Level INFO -ExitCode 0 -Message "Import-WACBuildSignerCertificate: Successfully imported the build signer certificate(s)."
}
else {
Write-Log -Level WARN -ExitCode 0 -Message "Import-WACBuildSignerCertificate: The configuration modules are not signed, cannot import the build signer certificate(s)."
}
ExitWithErrorCode 0
}
catch {
Write-Log -Level WARN -ExitCode 1 -Message "Import-WACBuildSignerCertificate: Failed to import the build signer certificate(s). Error: $_"
ExitWithErrorCode 1
throw
}
finally {
if ($null -ne $chain) {
$chain.Dispose()
$chain = $null
}
if ($null -ne $store) {
$store.Dispose()
$store = $null
}
}
}
<#
.SYNOPSIS
Registers the Windows Admin Center service.
.DESCRIPTION
Registers the Windows Admin Center service. Unregisters the service first if it is already registered.
.PARAMETER Automatic
Automatically start the service.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Register-WACService
.EXAMPLE
Register-WACService -Automatic
#>
function Register-WACService {
Param(
[Parameter(Mandatory = $false)]
[switch]$Automatic,
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
$service = Get-Service -Name $ConstServiceName -ErrorAction SilentlyContinue
if ($service.Length -gt 0) {
Unregister-WACService -ExitWithErrorCode:$false
}
$basePath = $ConstServiceFolderPath;
$path = Join-Path -Path $basePath -ChildPath $ConstExecutableName
$path = [System.IO.Path]::GetFullPath($path)
$networkServiceSid = New-Object System.Security.Principal.SecurityIdentifier -ArgumentList $ConstNetworkServiceSid
$networkServiceName = $networkServiceSid.Translate([System.Security.Principal.NTAccount]).Value
$startMode = if ($Automatic) { "auto" } else { "demand" }
Invoke-WACWinCommand -Command $ConstServiceController -Parameters "create", $ConstServiceName, "type=", "own", "start=", $startMode, "depend=", "winrm", "obj=", """$networkServiceName""", "binpath=", """$path""", "displayname=", """$ConstDisplayName"""
Invoke-WACWinCommand -Command $ConstServiceController -Parameters "description", $ConstServiceName, """$ConstServiceDescription"""
Write-Log -Level INFO -ExitCode 0 -Message "Register-WACService: Successfully registered Windows Admin Center service."
ExitWithErrorCode 0
}
<#
.SYNOPSIS
Registers the Windows Admin Center Account Management service.
.DESCRIPTION
Registers the Windows Admin Center Account Management service. Unregisters the service first if it is already registered.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Register-WACAccountManagementService
#>
function Register-WACAccountManagementService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
$service = Get-Service -Name $ConstAccountManagementServiceName -ErrorAction SilentlyContinue
if ($service.Length -gt 0) {
Unregister-WACAccountManagementService -ExitWithErrorCode:$false
}
$path = Join-Path -Path $ConstServiceFolderPath -ChildPath $ConstAccoutManagementExecutableName
$path = [System.IO.Path]::GetFullPath($path)
Invoke-WACWinCommand -Command $ConstServiceController -Parameters "create", $ConstAccountManagementServiceName, "type=", "own", "start=", "demand", "binpath=", """$path""", "displayname=", """$ConstAccountManagementServiceDisplayName"""
Invoke-WACWinCommand -Command $ConstServiceController -Parameters "description", $ConstAccountManagementServiceName, """$ConstAccountManagementServiceDescription"""
Invoke-WACWinCommand -Command $ConstServiceController -Parameters "sdset", $ConstAccountManagementServiceName, $ConstAccountManagementSecurityDescriptor
Write-Log -Level INFO -ExitCode 0 -Message "Register-WACAccountManagementService: Successfully registered Windows Admin Center Account Management service."
ExitWithErrorCode 0
}
<#
.SYNOPSIS
Registers the Windows Admin Center Updater scheduled task.
.DESCRIPTION
Registers the Windows Admin Center Updater scheduled task. Returns early if the scheduled task is already registered unless Force flag is set.
.PARAMETER Force
Force the registration of the service.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Register-WACUpdaterScheduledTask
.EXAMPLE
Register-WACUpdaterScheduledTask -Force
#>
function Register-WACUpdaterScheduledTask {
Param(
[Parameter(Mandatory = $false)]
[switch]$Force,
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
$service = Get-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName -ErrorAction SilentlyContinue
if ($service.Length -gt 0) {
if ($Force -eq $false) {
Write-Log -Level INFO -ExitCode 0 -Message "Register-WACUpdaterScheduledTask: Windows Admin Center Updater scheduled task is already registered, returning early."
ExitWithErrorCode 0
return
}
Unregister-WACUpdaterScheduledTask -ExitWithErrorCode:$false
}
$path = Join-Path -Path $ConstUpdaterFolderPath -ChildPath $ConstUpdaterExecutableName
$path = [System.IO.Path]::GetFullPath($path)
try {
$action = New-ScheduledTaskAction -Execute $path
Register-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName -Action $action -User $ConstSystemObject -RunLevel Highest -Force
$scheduler = New-Object -ComObject Schedule.Service
$scheduler.Connect()
$task = $scheduler.GetFolder("\").GetTask($ConstUpdaterScheduledTaskName)
$task.SetSecurityDescriptor($ConstUpdaterScheduledTaskSecurityDescriptor, 0)
}
catch {
Write-Log -Level ERROR -ExitCode 1 -Message "Register-WACUpdaterScheduledTask: Failed to register Windows Admin Center Updater scheduled task."
Write-Error $_
ExitWithErrorCode 1
throw
}
Write-Log -Level INFO -ExitCode 0 -Message "Register-WACUpdaterScheduledTask: Successfully registered Windows Admin Center Updater scheduled task."
ExitWithErrorCode 0
}
<#
.SYNOPSIS
Restarts the Windows Admin Center service.
.DESCRIPTION
Restarts the Windows Admin Center service.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Restart-WACService
#>
function Restart-WACService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
Restart-Service -Name $ConstServiceName -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Restart-WACService: Successfully restarted Windows Admin Center service."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Restart-WACService: Failed to restart Windows Admin Center service. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Restarts the Windows Admin Center Account Management service.
.DESCRIPTION
Restarts the Windows Admin Center Account Management service.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Restart-WACAccountManagementService
#>
function Restart-WACAccountManagementService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
Restart-Service -Name $ConstAccountManagementServiceName -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Restart-WACAccountManagementService: Successfully restarted Windows Admin Center Account Management service."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Restart-WACService: Failed to restart Windows Admin Center Account Management service. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Sets Network Service account access.
.DESCRIPTION
Sets Network Service account access to Full on %ProgramData%\WindowsAdminCenter folder and %ProgramFiles%\WindowsAdminCenter\Service\appsettings.json with inherited state.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Set-WACNetworkServiceAccess
#>
function Set-WACNetworkServiceAccess {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$networkServiceSid = New-Object System.Security.Principal.SecurityIdentifier -ArgumentList $ConstNetworkServiceSid
$acl = Get-Acl -Path $ConstProgramDataFolderPath
$networkService = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $networkServiceSid,$ConstFullControlPermissions,3,"None","Allow"
$acl.SetAccessRule($networkService)
Set-Acl -Path $ConstProgramDataFolderPath -AclObject $acl
$appSettingsPath = GetAppSettingsPath
$acl = Get-Acl -Path $appSettingsPath
$appSettings = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $networkServiceSid,$ConstFullControlPermissions,"Allow"
$acl.SetAccessRule($appSettings)
Set-Acl -Path $appSettingsPath -AclObject $acl
Write-Log -Level INFO -ExitCode 0 -Message "Set-WACNetworkServiceAccess: Configured access for Network Service to the data folder and the configuration file."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Set-WACNetworkServiceAccess: Failed to configure access for Network Service to the data folder and the configuration file. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Gets if the current OS has a desktop.
.DESCRIPTION
Gets if the current operating system has a desktop (e.g., Windows Client SKUs, non-core Windows Server SKUs, etc.).
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, 1 is error, 2 is early exit.
.EXAMPLE
Get-WACHasDesktop
#>
function Get-WACHasDesktop {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$productType = (Get-CimInstance -ClassName Win32_OperatingSystem).ProductType
if ($productType -ne 1) {
# check if it's Windows Workstation such as Windows Client ("1")
$foundDesktop = @(Dism /online /Get-Packages /Format:List | Where-Object {$_ -like "*Microsoft-Windows-UserExperience-Desktop-Package*" } ).Count -eq 1
if (-not $foundDesktop) {
Write-Log -Level INFO -ExitCode 2 -Message "Get-WACHasDesktop: Current OS does not have desktop."
ExitWithErrorCode 2
return $false
}
}
Write-Log -Level INFO -ExitCode 0 -Message "Get-WACHasDesktop: Current OS does have desktop."
ExitWithErrorCode 0
return $true
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Get-WACHasDesktop: Failed to determine if current OS has desktop. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Starts the Windows Admin Center service.
.DESCRIPTION
Starts the Windows Admin Center service.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Start-WACService
#>
function Start-WACService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
Start-Service -Name $ConstServiceName -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Start-WACService: Successfully started Windows Admin Center service."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Start-WACService: Failed to start Windows Admin Center service. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Starts Windows Admin Center through launcher.
.DESCRIPTION
Starts Windows Admin Center through launcher if the current operating system has a desktop (e.g., Windows Client SKUs, non-core Windows Server SKUs, etc.). Otherwise returns early.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, 1 is error, 2 is early exit.
.EXAMPLE
Start-WACLauncher
#>
function Start-WACLauncher {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$hasDesktop = Get-WACHasDesktop
if (-not $hasDesktop) {
Write-Log -Level INFO -ExitCode 2 -Message "Start-WACLauncher: Current OS does not have desktop. Exiting early."
ExitWithErrorCode 2
return
}
Start-Process -FilePath $ConstLauncherExecutableName -WorkingDirectory $ConstServiceFolderPath
Write-Log -Level INFO -ExitCode 0 -Message "Start-WACLauncher: Successfully started Windows Admin Center launcher."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Start-WACLauncher: Failed to start Windows Admin Center launcher. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Gets the status of the Windows Admin Center service.
.DESCRIPTION
Gets the status of the Windows Admin Center service and processes.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Get-WACService
#>
function Get-WACService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
Get-Service -Name $ConstServiceName -ErrorAction Stop
Get-Process -Name $ConstServiceName -ErrorAction SilentlyContinue
Write-Log -Level INFO -ExitCode 0 -Message "Get-WACService: Successfully got Windows Admin Center service status."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Get-WACService: Failed to get Windows Admin Center service status. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Gets the status of the Windows Admin Center Account Management service.
.DESCRIPTION
Gets the status of the Windows Admin Center Account Management service.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Get-WACAccountManagementService
#>
function Get-WACAccountManagementService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
Get-Service -Name $ConstAccountManagementServiceName -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Get-WACAccountManagementService: Successfully got Windows Admin Center Account Management service status."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Get-WACAccountManagementService: Failed to get Windows Admin Center Account Management service status. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Gets the status of Windows Admin Center Updater scheduled task.
.DESCRIPTION
Gets the status of Windows Admin Center Updater scheduled task.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Get-WACUpdaterScheduledTask
#>
function Get-WACUpdaterScheduledTask {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
Get-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Get-WACUpdaterScheduledTask: Successfully got Windows Admin Center Updater scheduled task status."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Get-WACUpdaterScheduledTask: Failed to get Windows Admin Center Updater scheduled task status. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Stops the Windows Admin Center service.
.DESCRIPTION
Stops the Windows Admin Center service if found on the system and is currently in the running state, otherwise returns early.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Stop-WACService
#>
function Stop-WACService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$wacService = Get-Service -Name $ConstServiceName -ErrorAction SilentlyContinue
if (($null -eq $wacService) -or ($wacService.Status -ne [System.ServiceProcess.ServiceControllerStatus]::Running)) {
Write-Log -Level INFO -ExitCode 0 -Message "Stop-WACService: Windows Admin Center service is already stopped or not available."
} else {
Stop-Service -Name $ConstServiceName -Force -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Stop-WACService: Successfully stopped Windows Admin Center service."
}
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Stop-WACService: Failed to stop Windows Admin Center. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Stops the Windows Admin Center launcher.
.DESCRIPTION
Stops the Windows Admin Center launcher if the current operating system has a desktop (e.g., Windows Client SKUs, non-core Windows Server SKUs, etc.) and the launcher process is launching. Otherwise returns early.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, 1 is error, 2 is early exit.
.EXAMPLE
Stop-WACLauncher
#>
function Stop-WACLauncher {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$hasDesktop = Get-WACHasDesktop
if (-not $hasDesktop) {
Write-Log -Level INFO -ExitCode 2 -Message "Stop-WACLauncher: Current OS does not have desktop. Exiting early."
ExitWithErrorCode 2
return
}
$launcher = Get-Process -Name $ConstLauncherName -ErrorAction SilentlyContinue
if ($null -ne $launcher) {
$launcher | Stop-Process -Force
Write-Log -Level INFO -ExitCode 0 -Message "Stop-WACLauncher: Successfully stopped Windows Admin Center Launcher."
}
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Stop-WACLauncher: Failed to stop Windows Admin Center Launcher. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Stops the Windows Admin Center Account Management service.
.DESCRIPTION
Stops the Windows Admin Center Account Management service if currently in the running state, otherwise returns early.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Stop-WACAccountManagementService
#>
function Stop-WACAccountManagementService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$wacService = Get-Service -Name $ConstAccountManagementServiceName
if ($wacService.Status -ne [System.ServiceProcess.ServiceControllerStatus]::Running) {
Write-Log -Level INFO -ExitCode 0 -Message "Stop-WACAccountManagementService: Windows Admin Center Account Management service is already stopped."
ExitWithErrorCode 0
return;
}
Stop-Service -Name $ConstAccountManagementServiceName -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Stop-WACAccountManagementService: Successfully stopped Windows Admin Center Account Management service."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Stop-WACAccountManagementService: Failed to stop Windows Admin Center Account Management service. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Stops the Windows Admin Center Updater scheduled task.
.DESCRIPTION
Stops the Windows Admin Center Updater scheduled task if currently in the running state, otherwise returns early.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Stop-WACUpdaterScheduledTask
#>
function Stop-WACUpdaterScheduledTask {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$updaterService = Get-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName
if ($updaterService.State -ne [System.ServiceProcess.ServiceControllerStatus]::Running) {
Write-Log -Level INFO -ExitCode 0 -Message "Stop-WACUpdaterScheduledTask: Windows Admin Center Updater scheduled task is already stopped."
ExitWithErrorCode 0
return;
}
Stop-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName -ErrorAction Stop
Write-Log -Level INFO -ExitCode 0 -Message "Stop-WACUpdaterScheduledTask: Successfully stopped Windows Admin Center Updater scheduled task."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Stop-WACUpdaterScheduledTask: Failed to stop Windows Admin Center Updater scheduled task. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Unregisters the Windows Admin Center service.
.DESCRIPTION
Stops the Windows Admin Center service if running and then unregisters it.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Unregister-WACService
#>
function Unregister-WACService {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
Stop-Service -Name $ConstServiceName -ErrorAction SilentlyContinue
Invoke-WACWinCommand -Command $ConstServiceController -Parameters "delete", $ConstServiceName
Write-Log -Level INFO -ExitCode 0 -Message "Unregister-WACService: Successfully unregistered Windows Admin Center service."
ExitWithErrorCode 0
}
<#
.SYNOPSIS
Unregisters the Windows Admin Center Account Management service.
.DESCRIPTION
Stops the Windows Admin Center Account Management service if running and then unregisters it. If the CheckIfExist flag is used and the service does not exist, returns early.
.PARAMETER CheckIfExist
Check if the service exists before attempting to unregister it.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Unregister-WACAccountManagementService
.EXAMPLE
Unregister-WACAccountManagementService -CheckIfExist
#>
function Unregister-WACAccountManagementService {
Param(
[Parameter(Mandatory = $false)]
[switch]$CheckIfExist,
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
if ($CheckIfExist) {
$service = Get-Service -Name $ConstAccountManagementServiceName -ErrorAction SilentlyContinue
if ($service.Length -eq 0) {
Write-Log -Level INFO -ExitCode 0 -Message "Unregister-WACAccountManagementService: Not found Windows Admin Center Account Management service."
ExitWithErrorCode 0
return
}
}
Stop-Service -Name $ConstAccountManagementServiceName -ErrorAction SilentlyContinue
Invoke-WACWinCommand -Command $ConstServiceController -Parameters "delete", $ConstAccountManagementServiceName
Write-Log -Level INFO -ExitCode 0 -Message "Unregister-WACAccountManagementService: Successfully unregistered Windows Admin Center Account Management service."
ExitWithErrorCode 0
}
<#
.SYNOPSIS
Unregisters the Windows Admin Center Updater scheduled task.
.DESCRIPTION
Stops the Windows Admin Center Updater scheduled task if running and then unregisters it.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Unregister-WACUpdaterScheduledTask
#>
function Unregister-WACUpdaterScheduledTask {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
Stop-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName -Confirm:$false
Write-Log -Level INFO -ExitCode 0 -Message "Unregister-WACUpdaterScheduledTask: Successfully unregistered Windows Admin Center Updater scheduled task."
ExitWithErrorCode 0
}
<#
.SYNOPSIS
Copies Windows Admin Center process files from installer from temp location to path to use for service registration.
.DESCRIPTION
Copies Windows Admin Center Updater process files from installer from temp location to path to use for scheduled task registration.
If the scheduled task is already registered, this function returns early in case the updater scheduled task is running the installer.
Setting the Force switch will force the copy to occur.
.PARAMETER Force
Force the copy to occur.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Copy-WACTempUpdaterProcessFiles
.EXAMPLE
Copy-WACTempUpdaterProcessFiles -Force
#>
function Copy-WACTempUpdaterProcessFiles {
Param(
[Parameter(Mandatory = $false)]
[switch]$Force,
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
$scheduledTask = Get-ScheduledTask -TaskName $ConstUpdaterScheduledTaskName -ErrorAction SilentlyContinue
if ($scheduledTask.Length -gt 0) {
if ($Force -eq $false) {
Write-Log -Level INFO -ExitCode 0 -Message "Copy-WACTempUpdaterProcessFiles: Windows Admin Center Updater scheduled task is already registered. Skipping copy."
ExitWithErrorCode 0
return
}
Stop-WACUpdaterScheduledTask -ExitWithErrorCode:$false
}
Copy-Item -Path "$ConstServiceFolderPath\*" -Destination $ConstUpdaterFolderPath `
-Exclude $("$ConstServiceName.*", "$ConstAccountManagementServiceName.*", "$ConstLauncherName.*", $ConstEntityFrameworkBundleFileName) -Recurse -Force
Copy-Item -Path "$ConstServiceFolderPath\$ConstUpdaterExecutableName" -Destination $ConstUpdaterFolderPath -Force
Write-Log -Level INFO -ExitCode 0 -Message "Copy-WACTempUpdaterProcessFiles: Successfully copied Windows Admin Center Updater process files."
ExitWithErrorCode 0
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "Copy-WACTempUpdaterProcessFiles: Failed to copy Windows Admin Center Updater process files. Error: $_"
ExitWithErrorCode 1
throw
}
}
<#
.SYNOPSIS
Creates the Windows Admin Center event log and configures it.
.DESCRIPTION
Creates the Windows Admin Center event log sources underneath the WindowsAdminCenter log name and configures the event logs underneath the log name.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
New-WACEventLog
#>
function New-WACEventLog {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)
SetExitWithErrorCode $ExitWithErrorCode
try {
if (!(AssertEventLogExists($ConstEventLogName))) {
CreateEventSources
}
}
catch {
Write-Error $_
Write-Log -Level ERROR -ExitCode 1 -Message "New-WACEventLog: Failed to create Windows Admin Center event log. Error: $_"
ExitWithErrorCode 1
throw
}
$hundredMegaByte = 100 * 1024 * 1024
Invoke-WACWinCommand -Command $ConstWevutilCommand -Parameters "set-log $ConstEventLogName /ms:$hundredMegaByte"
Write-Log -Level INFO -ExitCode 0 -Message "New-WACEventLog: Successfully created Windows Admin Center event log."
ExitWithErrorCode 0
}
<#
.SYNOPSIS
Removes the Windows Admin Center event log.
.DESCRIPTION
Removes the Windows Admin Center event log.
.PARAMETER ExitWithErrorCode
Exit the script with the error code. 0 is success, otherwise error.
.EXAMPLE
Remove-WACEventLog
#>
function Remove-WACEventLog {
Param(
[Parameter(Mandatory = $false)]
[switch]$ExitWithErrorCode
)