-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDeploy-ACMEServer.ADCS.ps1
More file actions
720 lines (577 loc) · 23.6 KB
/
Deploy-ACMEServer.ADCS.ps1
File metadata and controls
720 lines (577 loc) · 23.6 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
[CmdletBinding()]
param(
[Parameter(Mandatory, ParameterSetName = 'StepDownloadSoftware')]
[switch]$StepDownloadSoftware,
[Parameter(Mandatory, ParameterSetName = 'StepDownloadNetHostingPackage')]
[switch]$StepDownloadNetHostingPackage,
[Parameter(Mandatory, ParameterSetName = 'StepInstallHostingPackage')]
[switch]$StepInstallHostingPackage,
[Parameter(Mandatory, ParameterSetName = 'StepInstallIIS')]
[switch]$StepInstallIIS,
[Parameter(Mandatory, ParameterSetName = 'StepDeployToIIS')]
[switch]$StepDeployToIIS,
[Parameter(Mandatory, ParameterSetName = 'StepDeployAsService')]
[switch]$StepDeployAsService,
[Parameter(Mandatory, ParameterSetName = 'StepCreateKestrelConfig')]
[switch]$StepCreateKestrelConfig,
[Parameter(Mandatory, ParameterSetName = 'StepCreateConfig')]
[switch]$StepCreateConfig,
[Parameter(Mandatory, ParameterSetName = 'IIS')]
[switch]$IIS,
[Parameter(Mandatory, ParameterSetName = 'Service')]
[switch]$Service,
[Parameter(ParameterSetName = 'StepDownloadSoftware')]
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[switch]$UseBetaSoftware,
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[Parameter(ParameterSetName = 'StepDeployToIIS')]
[Parameter(ParameterSetName = 'StepDeployAsService')]
[PSCredential]$ProcessIdentityCredential,
[Parameter(ParameterSetName = 'Service')]
[Parameter(ParameterSetName = 'StepDeployAsService')]
[switch]$UseNetworkService,
[Parameter(Mandatory, ParameterSetName = 'StepDeployToIIS')]
[Parameter(Mandatory, ParameterSetName = 'StepCreateKestrelConfig')]
[Parameter(Mandatory, ParameterSetName = 'IIS')]
[Parameter(Mandatory, ParameterSetName = 'Service')]
[string]$DnsHostName,
[Parameter(ParameterSetName = 'StepCreateKestrelConfig')]
[Parameter(ParameterSetName = 'Service')]
[int]$KestrelHttpPort = 8080,
[Parameter(ParameterSetName = 'StepCreateKestrelConfig')]
[Parameter(ParameterSetName = 'Service')]
[int]$KestrelHttpsPort = 8443,
[Parameter(ParameterSetName = 'StepDownloadSoftware')]
[Parameter(ParameterSetName = 'StepDeployToIIS')]
[Parameter(ParameterSetName = 'StepDeployAsService')]
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[string]$SoftwareArchivePath = 'C:\Temp\acme-server.zip',
[Parameter(ParameterSetName = 'StepDownloadNetHostingPackage')]
[Parameter(ParameterSetName = 'StepInstallHostingPackage')]
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[string]$NetHostingInstallerPath = 'C:\Temp\dotnet-hosting.exe',
[Parameter(ParameterSetName = 'StepInstallHostingPackage')]
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[string]$NetHostingInstallerArguments = '/install /quiet /norestart',
[Parameter(ParameterSetName = 'StepDeployToIIS')]
[Parameter(ParameterSetName = 'StepDeployAsService')]
[Parameter(ParameterSetName = 'StepCreateKestrelConfig')]
[Parameter(ParameterSetName = 'StepCreateConfig')]
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[string]$DeploymentPath = 'C:\Program Files\th11s\acme-adcs',
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[switch]$SkipSoftwareDownload,
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[switch]$SkipHostingPackageDownload,
[Parameter(ParameterSetName = 'IIS')]
[Parameter(ParameterSetName = 'Service')]
[switch]$SkipHostingBundleInstall
)
begin {
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$script:OriginalInformationPreference = $InformationPreference
$InformationPreference = 'Continue'
function Get-LatestAcmeSoftwareAsset {
[CmdletBinding()]
param(
[switch]$IncludeBeta
)
$headers = @{
'Accept' = 'application/vnd.github+json'
'User-Agent' = 'ACME-Server-ADCS-DeployScript'
}
if ($IncludeBeta) {
$releases = Invoke-RestMethod -Method Get -Headers $headers -Uri 'https://api.github.com/repos/glatzert/ACME-Server-ADCS/releases?per_page=30'
$release = $releases | Where-Object { -not $_.draft -and $_.prerelease } | Select-Object -First 1
if (-not $release) {
throw 'No beta release found for glatzert/ACME-Server-ADCS.'
}
}
else {
$release = Invoke-RestMethod -Method Get -Headers $headers -Uri 'https://api.github.com/repos/glatzert/ACME-Server-ADCS/releases/latest'
}
$assets = $release.assets | Where-Object {
$_.name -match '\.zip$'
}
if (-not $assets) {
throw "No downloadable software asset found in release '$($release.tag_name)'."
}
return $assets
}
function Download-Software {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$DestinationPath,
[switch]$UseBeta
)
$assets = @(Get-LatestAcmeSoftwareAsset -IncludeBeta:$UseBeta)
$asset = $null
if ($assets.Count -eq 1) {
$asset = $assets[0]
}
else {
Write-Information 'Multiple software ZIP assets found. Please choose one:'
for ($index = 0; $index -lt $assets.Count; $index++) {
Write-Information ("[{0}] {1}" -f ($index + 1), $assets[$index].name)
}
do {
$selectionInput = Read-Host "Enter asset number (1-$($assets.Count))"
$selection = 0
$isValidNumber = [int]::TryParse($selectionInput, [ref]$selection)
}
while (-not $isValidNumber -or $selection -lt 1 -or $selection -gt $assets.Count)
$asset = $assets[$selection - 1]
}
$destinationDirectory = Split-Path -Path $DestinationPath -Parent
if (-not [string]::IsNullOrWhiteSpace($destinationDirectory) -and -not (Test-Path -Path $destinationDirectory)) {
New-Item -Path $destinationDirectory -ItemType Directory -Force | Out-Null
}
Write-Information ("Downloading software asset '{0}' from '{1}' to '{2}'..." -f $asset.name, $asset.browser_download_url, $DestinationPath)
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $DestinationPath
Write-Information 'Software download complete.'
}
function Get-LatestLtsNetHostingBundleUri {
[CmdletBinding()]
param()
$releaseIndexUri = 'https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/releases-index.json'
$index = Invoke-RestMethod -Method Get -Uri $releaseIndexUri
$ltsChannels = $index.'releases-index' |
Where-Object { $_.'release-type' -eq 'lts' -and $_.'support-phase' -eq 'active' } |
Sort-Object { [version]$_.'channel-version' } -Descending
if (-not $ltsChannels) {
throw 'No active LTS channels found in .NET release metadata.'
}
$selectedChannels = $ltsChannels
if ($ltsChannels.Count -gt 1) {
Write-Information 'Multiple active .NET LTS channels are available. Please choose one:'
for ($index = 0; $index -lt $ltsChannels.Count; $index++) {
$channel = $ltsChannels[$index]
Write-Information ("[{0}] .NET {1} (latest release: {2})" -f ($index + 1), $channel.'channel-version', $channel.'latest-release')
}
do {
$selectionInput = Read-Host "Enter LTS channel number (1-$($ltsChannels.Count))"
$selection = 0
$isValidNumber = [int]::TryParse($selectionInput, [ref]$selection)
}
while (-not $isValidNumber -or $selection -lt 1 -or $selection -gt $ltsChannels.Count)
$selectedChannels = @($ltsChannels[$selection - 1])
}
foreach ($channel in $selectedChannels) {
$channelReleases = Invoke-RestMethod -Method Get -Uri $channel.'releases.json'
$release = $channelReleases.releases | Where-Object { $_.'release-version' -eq $channel.'latest-release' } | Select-Object -First 1
if (-not $release) {
$release = $channelReleases.releases | Select-Object -First 1
}
if (-not $release) {
continue
}
$candidateFiles = @()
if ($release.'aspnetcore-runtime' -and $release.'aspnetcore-runtime'.files) {
$candidateFiles += $release.'aspnetcore-runtime'.files
}
if ($release.sdk -and $release.sdk.files) {
$candidateFiles += $release.sdk.files
}
if ($release.sdks) {
foreach ($sdk in $release.sdks) {
if ($sdk.files) {
$candidateFiles += $sdk.files
}
}
}
$hostingBundle = $candidateFiles |
Where-Object {
$_.name -eq 'dotnet-hosting-win.exe' -or $_.name -like 'dotnet-hosting-*-win.exe'
} |
Select-Object -First 1
if ($hostingBundle -and $hostingBundle.url) {
Write-Information ("Resolved latest LTS .NET Hosting Bundle from channel {0} release {1}." -f $channel.'channel-version', $release.'release-version')
return $hostingBundle.url
}
}
throw 'Unable to resolve .NET Hosting Bundle URL from latest LTS release metadata.'
}
function Download-NetHostingPackage {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$DestinationPath
)
$destinationDirectory = Split-Path -Path $DestinationPath -Parent
if (-not [string]::IsNullOrWhiteSpace($destinationDirectory) -and -not (Test-Path -Path $destinationDirectory)) {
New-Item -Path $destinationDirectory -ItemType Directory -Force | Out-Null
}
$downloadUri = Get-LatestLtsNetHostingBundleUri
Write-Information "Downloading .NET Hosting Bundle from '$downloadUri' to '$DestinationPath'..."
Invoke-WebRequest -Uri $downloadUri -OutFile $DestinationPath
Write-Information '.NET Hosting Bundle download complete.'
}
function Install-HostingPackage {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$InstallerPath,
[string]$InstallerArguments = '/install /quiet /norestart'
)
if (-not (Test-Path -Path $InstallerPath)) {
throw "Hosting installer not found at '$InstallerPath'."
}
Write-Information "Installing hosting package from '$InstallerPath'..."
$process = Start-Process -FilePath $InstallerPath -ArgumentList $InstallerArguments -Wait -PassThru
if ($process.ExitCode -ne 0) {
throw "Hosting package installation failed with exit code $($process.ExitCode)."
}
Write-Information 'Hosting package installation complete.'
}
function Install-IIS {
[CmdletBinding()]
param()
Write-Information 'Installing IIS Web Server + IIS Management Console...'
if (Get-Command -Name Install-WindowsFeature -ErrorAction SilentlyContinue) {
$result = Install-WindowsFeature -Name Web-Server, Web-Mgmt-Console
if (-not $result.Success) {
throw 'Failed to install IIS features with Install-WindowsFeature.'
}
}
elseif (Get-Command -Name Enable-WindowsOptionalFeature -ErrorAction SilentlyContinue) {
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole -All -NoRestart | Out-Null
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole -All -NoRestart | Out-Null
}
else {
throw 'Neither Install-WindowsFeature nor Enable-WindowsOptionalFeature is available on this system.'
}
Write-Information 'IIS installation step complete.'
}
function Extract-Software {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ArchivePath,
[Parameter(Mandatory)]
[string]$DestinationFolder
)
if (-not (Test-Path -Path $ArchivePath)) {
throw "Software archive not found at '$ArchivePath'."
}
if (-not (Test-Path -Path $DestinationFolder)) {
New-Item -Path $DestinationFolder -ItemType Directory -Force | Out-Null
}
Write-Information "Extracting software archive '$ArchivePath' to '$DestinationFolder'..."
Expand-Archive -Path $ArchivePath -DestinationPath $DestinationFolder -Force
Write-Information 'Software extraction complete.'
}
function Test-DnsHostName {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$HostName
)
if ($HostName -match '^(?=.{1,253}$)(?!-)[a-zA-Z0-9-]{1,63}(?<!-)(\.(?!-)[a-zA-Z0-9-]{1,63}(?<!-))*$') {
return $true
}
return $false
}
function Grant-ProcessIdentityAccess {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$DirectoryPath,
[Parameter(Mandatory)]
[string]$ProcessIdentity,
[Parameter(Mandatory)]
[System.Security.AccessControl.FileSystemRights]$FileSystemRights
)
if (-not (Test-Path -Path $DirectoryPath)) {
throw "Directory '$DirectoryPath' does not exist."
}
$inheritanceFlags = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
$propagationFlags = [System.Security.AccessControl.PropagationFlags]::None
$accessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$ProcessIdentity,
$FileSystemRights,
$inheritanceFlags,
$propagationFlags,
$accessControlType
)
$acl = Get-Acl -Path $DirectoryPath
$acl.SetAccessRule($accessRule)
Set-Acl -Path $DirectoryPath -AclObject $acl
}
function Add-IisProcessIdentityToIisIusrs {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ProcessIdentity
)
try {
Add-LocalGroupMember -Group 'IIS_IUSRS' -Member $ProcessIdentity -ErrorAction Stop
Write-Information "Added '$ProcessIdentity' to IIS_IUSRS."
}
catch {
if ($_.Exception.Message -match 'already a member') {
Write-Information "'$ProcessIdentity' is already in IIS_IUSRS."
}
else {
throw
}
}
}
function Deploy-SoftwareToIIS {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ArchivePath,
[Parameter(Mandatory)]
[string]$BindingHostName,
[Parameter(Mandatory)]
[PSCredential]$ProcessIdentityCredential,
[string]$DestinationFolder = 'C:\inetpub\th11s-acme-adcs',
[string]$SiteName = 'th11s-acme-adcs',
[string]$AppPoolName = 'th11s-acme-adcs',
[int]$Port = 80
)
if (-not (Test-DnsHostName -HostName $BindingHostName)) {
throw "Binding host name '$BindingHostName' must be a DNS name only."
}
$processIdentity = $ProcessIdentityCredential.UserName
$processPassword = $ProcessIdentityCredential.GetNetworkCredential().Password
Extract-Software -ArchivePath $ArchivePath -DestinationFolder $DestinationFolder
Add-IisProcessIdentityToIisIusrs -ProcessIdentity $processIdentity
Grant-ProcessIdentityAccess -DirectoryPath $DestinationFolder -ProcessIdentity $processIdentity -FileSystemRights ([System.Security.AccessControl.FileSystemRights]::ReadAndExecute)
Import-Module WebAdministration -ErrorAction Stop
if (-not (Test-Path "IIS:\AppPools\$AppPoolName")) {
New-WebAppPool -Name $AppPoolName | Out-Null
}
Set-ItemProperty -Path "IIS:\AppPools\$AppPoolName" -Name managedRuntimeVersion -Value ''
Set-ItemProperty -Path "IIS:\AppPools\$AppPoolName" -Name processModel -Value @{ identityType = 3; userName = $processIdentity; password = $processPassword }
if (-not (Test-Path "IIS:\Sites\$SiteName")) {
New-Website -Name $SiteName -PhysicalPath $DestinationFolder -ApplicationPool $AppPoolName -Port $Port -HostHeader $BindingHostName | Out-Null
}
else {
Set-ItemProperty -Path "IIS:\Sites\$SiteName" -Name physicalPath -Value $DestinationFolder
Set-ItemProperty -Path "IIS:\Sites\$SiteName" -Name applicationPool -Value $AppPoolName
$existingBinding = Get-WebBinding -Name $SiteName -Protocol http | Where-Object { $_.bindingInformation -eq "*:${Port}:$BindingHostName" }
if (-not $existingBinding) {
New-WebBinding -Name $SiteName -Protocol http -Port $Port -HostHeader $BindingHostName | Out-Null
}
}
Write-Information "IIS deployment completed. Site '$SiteName' is bound to '$BindingHostName'."
}
function New-AcmeWindowsService {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ExecutablePath,
[Parameter(Mandatory)]
[string]$ServiceName,
[Parameter(Mandatory)]
[string]$DisplayName,
[PSCredential]$ProcessIdentityCredential,
[switch]$UseNetworkService
)
$existingService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($existingService) {
if ($existingService.Status -eq 'Running') {
Stop-Service -Name $ServiceName -Force
}
$serviceInstance = Get-CimInstance -ClassName Win32_Service -Filter "Name='$ServiceName'" -ErrorAction SilentlyContinue
if ($serviceInstance) {
$deleteResult = Invoke-CimMethod -InputObject $serviceInstance -MethodName Delete
if ($deleteResult.ReturnValue -ne 0) {
throw "Failed to delete existing Windows service '$ServiceName'. ReturnValue: $($deleteResult.ReturnValue)"
}
}
Start-Sleep -Seconds 2
}
$binaryPath = "`"$ExecutablePath`""
if ($UseNetworkService) {
New-Service -Name $ServiceName -BinaryPathName $binaryPath -DisplayName $DisplayName -StartupType Automatic | Out-Null
sc.exe config $ServiceName obj= "NT AUTHORITY\Network Service" | Out-Null
Write-Information "Windows service '$ServiceName' created using Network Service account."
}
else {
if (-not $ProcessIdentityCredential) {
throw "ProcessIdentityCredential is required when not using Network Service."
}
New-Service -Name $ServiceName -BinaryPathName $binaryPath -DisplayName $DisplayName -StartupType Automatic -Credential $ProcessIdentityCredential | Out-Null
Write-Information "Windows service '$ServiceName'."
Write-Warning "Before you can start the service, the accounts needs 'Logon as Service' rights - grant it via LocalGroupPolicy or GroupPolicy, if necessary";
Write-Warning "The service account is: $($ProcessIdentityCredential.UserName)"
}
}
function Deploy-SoftwareAsService {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ArchivePath,
[PSCredential]$ProcessIdentityCredential,
[switch]$UseNetworkService,
[string]$DestinationFolder = 'C:\Program Files\th11s\acme-adcs',
[string]$ServiceName = 'ACMEServer.ADCS',
[string]$ServiceDisplayName = 'ACME Service ADCS'
)
Extract-Software -ArchivePath $ArchivePath -DestinationFolder $DestinationFolder
if ($UseNetworkService) {
$processIdentityName = 'NT AUTHORITY\Network Service'
}
else {
if (-not $ProcessIdentityCredential) {
throw "ProcessIdentityCredential is required when not using Network Service."
}
$processIdentityName = $ProcessIdentityCredential.UserName
}
Grant-ProcessIdentityAccess -DirectoryPath $DestinationFolder -ProcessIdentity $processIdentityName -FileSystemRights ([System.Security.AccessControl.FileSystemRights]::Modify)
$exePath = Join-Path -Path $DestinationFolder -ChildPath 'ACMEServer.ADCS.exe'
if (-not (Test-Path -Path $exePath)) {
throw "Service executable not found at '$exePath'."
}
New-AcmeWindowsService -ExecutablePath $exePath -ServiceName $ServiceName -DisplayName $ServiceDisplayName -ProcessIdentityCredential $ProcessIdentityCredential -UseNetworkService:$UseNetworkService
return $ServiceName
}
function New-KestrelConfiguration {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$DnsHostName,
[Parameter(Mandatory)]
[string]$DestinationPath,
[int]$HttpPort = 8080,
[int]$HttpsPort = 8443
)
$configPath = Join-Path -Path $DestinationPath -ChildPath 'appsettings.Kestrel.json'
if (-not (Test-Path -Path $DestinationPath)) {
New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null
}
$kestrelConfig = @{
Kestrel = @{
Endpoints = @{
Http = @{
Url = "http://$($DnsHostName):$HttpPort"
}
Https = @{
Url = "https://$($DnsHostName):$HttpsPort"
Certificate = @{
Store = @{
Name = "My"
Location = "LocalMachine"
}
Subject = $DnsHostName
AllowInvalid = $false
}
}
}
}
}
$json = $kestrelConfig | ConvertTo-Json -Depth 10
Set-Content -Path $configPath -Value $json -Encoding UTF8 -Force
Write-Information "Kestrel configuration created at '$configPath' with certificate subject '$DnsHostName'."
}
function Invoke-ConfigurationTool {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$DeploymentPath
)
if (-not (Test-Path -Path $DeploymentPath)) {
throw "Deployment path '$DeploymentPath' does not exist. Please deploy the software first."
}
$cliExecutable = Join-Path -Path $DeploymentPath -ChildPath 'ACMEServer.CLI.exe'
if (-not (Test-Path -Path $cliExecutable)) {
throw "CLI executable not found at '$cliExecutable'."
}
Write-Information "Starting configuration creation tool from '$DeploymentPath'..."
& $cliExecutable --config-tool
if ($LASTEXITCODE -ne 0) {
Write-Warning "Configuration tool exited with code $LASTEXITCODE. Configuration may not have been created."
}
else {
Write-Information "Configuration tool completed successfully."
}
}
} # end begin block
end {
$InformationPreference = $script:OriginalInformationPreference
}
process {
$parameterSetsWithProcessIdentity = @('StepDeployToIIS', 'StepDeployAsService', 'IIS', 'Service')
if ($PSCmdlet.ParameterSetName -in $parameterSetsWithProcessIdentity) {
if (-not $ProcessIdentityCredential -and -not $UseNetworkService) {
$ProcessIdentityCredential = Get-Credential -Message "Enter credentials for the ACME service process identity"
if (-not $ProcessIdentityCredential) {
throw 'Process identity credentials are required for deployment when not using Network Service.'
}
}
}
# Dynamically assign DeploymentPath based on deployment mode if using default
if ($DeploymentPath -eq 'C:\Program Files\th11s\acme-adcs') {
if ($PSCmdlet.ParameterSetName -in @('IIS', 'StepDeployToIIS')) {
$DeploymentPath = 'C:\inetpub\th11s-acme-adcs'
}
}
switch ($PSCmdlet.ParameterSetName) {
'StepDownloadSoftware' {
Download-Software -DestinationPath $SoftwareArchivePath -UseBeta:$UseBetaSoftware
}
'StepDownloadNetHostingPackage' {
Download-NetHostingPackage -DestinationPath $NetHostingInstallerPath
}
'StepInstallHostingPackage' {
Install-HostingPackage -InstallerPath $NetHostingInstallerPath -InstallerArguments $NetHostingInstallerArguments
}
'StepInstallIIS' {
Install-IIS
}
'StepDeployToIIS' {
Deploy-SoftwareToIIS -ArchivePath $SoftwareArchivePath -BindingHostName $DnsHostName -ProcessIdentityCredential $ProcessIdentityCredential -DestinationFolder $DeploymentPath
}
'StepDeployAsService' {
Deploy-SoftwareAsService -ArchivePath $SoftwareArchivePath -ProcessIdentityCredential $ProcessIdentityCredential -UseNetworkService:$UseNetworkService -DestinationFolder $DeploymentPath
}
'StepCreateKestrelConfig' {
New-KestrelConfiguration -DnsHostName $DnsHostName -DestinationPath $DeploymentPath -HttpPort $KestrelHttpPort -HttpsPort $KestrelHttpsPort
}
'StepCreateConfig' {
Invoke-ConfigurationTool -DeploymentPath $DeploymentPath
}
'IIS' {
if (-not $SkipSoftwareDownload) {
Download-Software -DestinationPath $SoftwareArchivePath -UseBeta:$UseBetaSoftware
}
Install-IIS
if (-not $SkipHostingBundleInstall) {
if (-not $SkipHostingPackageDownload) {
Download-NetHostingPackage -DestinationPath $NetHostingInstallerPath
}
Install-HostingPackage -InstallerPath $NetHostingInstallerPath -InstallerArguments $NetHostingInstallerArguments
}
Deploy-SoftwareToIIS -ArchivePath $SoftwareArchivePath -BindingHostName $DnsHostName -ProcessIdentityCredential $ProcessIdentityCredential -DestinationFolder $DeploymentPath
Invoke-ConfigurationTool -DeploymentPath $DeploymentPath
}
'Service' {
if (-not $SkipSoftwareDownload) {
Download-Software -DestinationPath $SoftwareArchivePath -UseBeta:$UseBetaSoftware
}
if (-not $SkipHostingBundleInstall) {
if (-not $SkipHostingPackageDownload) {
Download-NetHostingPackage -DestinationPath $NetHostingInstallerPath
}
Install-HostingPackage -InstallerPath $NetHostingInstallerPath -InstallerArguments $NetHostingInstallerArguments
}
New-KestrelConfiguration -DnsHostName $DnsHostName -DestinationPath $DeploymentPath -HttpPort $KestrelHttpPort -HttpsPort $KestrelHttpsPort
$ServiceName = Deploy-SoftwareAsService -ArchivePath $SoftwareArchivePath -ProcessIdentityCredential $ProcessIdentityCredential -UseNetworkService:$UseNetworkService -DestinationFolder $DeploymentPath
Invoke-ConfigurationTool -DeploymentPath $DeploymentPath
Write-Information "Use PS> 'Start-Service -Name $ServiceName' to start the service"
}
}
} # end process block