-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.ps1
631 lines (560 loc) · 27.4 KB
/
build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
[CmdletBinding(DefaultParameterSetName = 'task')]
Param (
[Parameter(
Mandatory = $False,
ParameterSetName = 'task',
Position = 0
)]
[string[]]$Task = 'Default',
[Parameter(Mandatory = $False)]
[string]$CertFileForSignature,
[Parameter(Mandatory = $False)]
[ValidateNotNullorEmpty()]
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Cert,
[Parameter(Mandatory = $False)]
[pscredential]$AdminUserCreds,
[Parameter(
Mandatory = $False,
ParameterSetName = 'help'
)]
[switch]$Help,
[Parameter(Mandatory = $False)]
[switch]$AppVeyorContext
)
# Workflow is build.ps1 -> psake.ps1 -> *Tests.ps1
##### BEGIN Prepare For Build #####
$ElevationCheck = [System.Security.Principal.WindowsPrincipal]::new([System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
if (!$ElevationCheck) {
Write-Error "You must run the build.ps1 as an Administrator (i.e. elevated PowerShell Session)! Halting!"
$global:FunctionResult = "1"
return
}
if ($AdminUserCreds) {
# Make sure $AdminUserCreds.UserName format is <Domain>\<User> and <LocalHostName>\<User>
if ($AdminUserCreds.UserName -notmatch "[\w]+\\[\w]+") {
Write-Error "The UserName provided in the PSCredential -AdminUserCreds is not in the correct format! Please create the PSCredential with a UserName in the format <Domain>\<User> or <LocalHostName>\<User>. Halting!"
$global:FunctionResult = "1"
return
}
}
if (!$(Get-Module -ListAvailable PSDepend)) {
& $(Resolve-Path "$PSScriptRoot\*Help*\Install-PSDepend.ps1").Path
}
try {
Import-Module PSDepend
$null = Invoke-PSDepend -Path "$PSScriptRoot\build.requirements.psd1" -Install -Import -Force
# Hack to fix AppVeyor Error When attempting to Publish to PSGallery
# The specific error this fixes is a problem with the Publish-Module cmdlet from PowerShellGet. PSDeploy
# calls Publish-Module without the -Force parameter which results in this error: https://github.com/PowerShell/PowerShellGet/issues/79
# This is more a problem with PowerShellGet than PSDeploy.
<#
Remove-Module PSDeploy
$PSDeployScriptToEdit = Get-Childitem -Path $(Get-Module -ListAvailable PSDeploy).ModuleBase -File -Recurse -Filter "PSGalleryModule.ps1"
[System.Collections.ArrayList][array]$PSDeployScriptContent = Get-Content $PSDeployScriptToEdit.FullName
$LineOfInterest = $($PSDeployScriptContent | Select-String -Pattern ".*?Verbose[\s]+= \`$VerbosePreference").Matches.Value
$IndexOfLineOfInterest = $PSDeployScriptContent.IndexOf($LineOfInterest)
$PSDeployScriptContent.Insert($($IndexOfLineOfInterest+1)," Force = `$True")
Set-Content -Path $PSDeployScriptToEdit.FullName -Value $PSDeployScriptContent
#>
Import-Module PSDeploy
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
Set-BuildEnvironment -Force -Path $PSScriptRoot -ErrorAction SilentlyContinue
# Now the following Environment Variables with similar values should be available to use...
<#
$env:BHBuildSystem = "Unknown"
$env:BHProjectPath = "U:\powershell\ProjectRepos\Sudo"
$env:BHBranchName = "master"
$env:BHCommitMessage = "!deploy"
$env:BHBuildNumber = 0
$env:BHProjectName = "Sudo"
$env:BHPSModuleManifest = "U:\powershell\ProjectRepos\Sudo\Sudo\Sudo.psd1"
$env:BHModulePath = "U:\powershell\ProjectRepos\Sudo\Sudo"
$env:BHBuildOutput = "U:\powershell\ProjectRepos\Sudo\BuildOutput"
Write-Host "`$env:BHBuildSystem is $env:BHBuildSystem"
Write-Host "`$env:BHProjectPath is $env:BHProjectPath"
Write-Host "`$env:BHBranchName is $env:BHBranchName"
Write-Host "`$env:BHCommitMessage is $env:BHCommitMessage"
Write-Host "`$env:BHBuildNumber is $env:BHBuildNumber"
Write-Host "`$env:BHProjectName is $env:BHProjectName"
Write-Host "`$env:BHPSModuleManifest is $env:BHPSModuleManifest"
Write-Host "`$env:BHModulePath is $env:BHModulePath"
Write-Host "`$env:BHBuildOutput is $env:BHBuildOutput"
#>
# Make sure everything is valid PowerShell before continuing...
$FilesToAnalyze = Get-ChildItem $PSScriptRoot -Recurse -File | Where-Object {
$_.Extension -match '\.ps1|\.psm1|\.psd1'
}
[System.Collections.ArrayList]$InvalidPowerShell = @()
foreach ($FileItem in $FilesToAnalyze) {
$contents = Get-Content -Path $FileItem.FullName -ErrorAction Stop
$errors = $null
$null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
if ($errors.Count -gt 0 -and $FileItem.Name -ne "$env:BHProjectName.psm1") {
$null = $InvalidPowerShell.Add($FileItem)
}
}
if ($InvalidPowerShell.Count -gt 0) {
Write-Error "The following files are not valid PowerShell:`n$($InvalidPowerShell.FullName -join "`n")`nHalting!"
$global:FunctionResult = "1"
return
}
#region >> Sign Everything
if ($CertFileForSignature -and !$Cert) {
if (!$(Test-Path $CertFileForSignature)) {
Write-Error "Unable to find the Certificate specified to be used for Code Signing! Halting!"
$global:FunctionResult = "1"
return
}
try {
$Cert = Get-PfxCertificate $CertFileForSignature -ErrorAction Stop
if (!$Cert) {throw "There was a prblem with the Get-PfcCertificate cmdlet! Halting!"}
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
}
if ($Cert) {
# Make sure the Cert is good for Code Signing
if ($Cert.EnhancedKeyUsageList.ObjectId -notcontains "1.3.6.1.5.5.7.3.3") {
$CNOfCert = $($($Cert.Subject -split ",")[0] -replace "CN=","").Trim()
Write-Error "The provided Certificate $CNOfCert says that it should be sued for $($Cert.EnhancedKeyUsageList.FriendlyName -join ','), NOT 'Code Signing'! Halting!"
$global:FunctionResult = "1"
return
}
# Make sure our ProtoHelpers are signed before we do anything else, otherwise we won't be able to use them
$HelperFilestoSign = Get-ChildItem $(Resolve-Path "$PSScriptRoot\*Help*\").Path -Recurse -File | Where-Object {
$_.Extension -match '\.ps1|\.psm1|\.psd1|\.ps1xml' -and $_.Name -ne "Remove-Signature.ps1"
}
# Before we loop through and sign the Helper functions, we need to sign Remove-Signature.ps1
$RemoveSignatureFilePath = $(Resolve-Path "$PSScriptRoot\*Help*\Remove-Signature.ps1").Path
if (!$(Test-Path $RemoveSignatureFilePath)) {
Write-Error "Unable to find the path $RemoveSignatureFilePath! Halting!"
$global:FunctionResult = "1"
return
}
# Because Set-Authenticode sometimes eats a trailing line when it is used, make sure Remove-Signature.ps1 doesn't break
$SingatureLineRegex = '^# SIG # Begin signature block|^<!-- SIG # Begin signature block -->'
$RemoveSignatureContent = Get-Content $RemoveSignatureFilePath
[System.Collections.ArrayList]$UpdatedRemoveSignatureContent = @()
foreach ($line in $RemoveSignatureContent) {
if ($line -match $SingatureLineRegex) {
$null = $UpdatedRemoveSignatureContent.Add("`n")
break
}
else {
$null = $UpdatedRemoveSignatureContent.Add($line)
}
}
Set-Content -Path $RemoveSignatureFilePath -Value $UpdatedRemoveSignatureContent
try {
$SetAuthenticodeResult = Set-AuthenticodeSignature -FilePath $RemoveSignatureFilePath -Cert $Cert
if (!$SetAuthenticodeResult -or $SetAuthenticodeResult.Status -ne "Valid") {throw "There was a problem using the Set-AuthenticodeSignature cmdlet to sign the Remove-Signature.ps1 function! Halting!"}
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
# Dot Source the Remove-Signature function
. $RemoveSignatureFilePath
if (![bool]$(Get-Item Function:\Remove-Signature)) {
Write-Error "Problem dot sourcing the Remove-Signature function! Halting!"
$global:FunctionResult = "1"
return
}
# Loop through the Help Scripts/Functions and sign them so that we can use them immediately if necessary
Remove-Signature -FilePath $HelperFilestoSign.FullName
[System.Collections.ArrayList]$FilesFailedToSign = @()
foreach ($FilePath in $HelperFilestoSign.FullName) {
try {
Write-Host "Signing $FilePath..."
$SetAuthenticodeResult = Set-AuthenticodeSignature -FilePath $FilePath -cert $Cert
if (!$SetAuthenticodeResult -or $SetAuthenticodeResult.Status -ne "Valid") {throw}
}
catch {
Write-Warning "Signing $FilePath failed!"
$null = $FilesFailedToSign.Add($FilePath)
}
}
if ($FilesFailedToSign.Count -gt 0) {
Write-Error "Halting because we failed to digitally sign the following files:`n$($FilesFailedToSign -join "`n")"
$global:FunctionResult = "1"
return
}
}
if ($Cert) {
# NOTE: We don't want to include the Module's .psm1 or .psd1 yet because the psake.ps1 Compile Task hasn't finalized them yet...
# NOTE: We don't want to sign build.ps1, Remove-Signature.ps1, or Helper functions because we just did that above...
$HelperFilesToSignNameRegex = $HelperFilestoSign.Name | foreach {[regex]::Escape($_)}
$RemoveSignatureFilePathRegex = [regex]::Escape($RemoveSignatureFilePath)
[System.Collections.ArrayList][array]$FilesToSign = Get-ChildItem $env:BHProjectPath -Recurse -File | Where-Object {
$_.Extension -match '\.ps1|\.psm1|\.psd1|\.ps1xml' -and
$_.Name -notmatch "^$env:BHProjectName\.ps[d|m]1$" -and
$_.Name -notmatch "^module\.requirements\.psd1" -and
$_.Name -notmatch "^build\.requirements\.psd1" -and
$_.Name -notmatch "^build\.ps1$" -and
$_.Name -notmatch $($HelperFilesToSignNameRegex -join '|') -and
$_.Name -notmatch $RemoveSignatureFilePathRegex -and
$_.FullName -notmatch "\\Pages\\Dynamic|\\Pages\\Static"
}
#$null = $FilesToSign.Add($(Get-Item $env:BHModulePath\Install-PSDepend.ps1))
Remove-Signature -FilePath $FilesToSign.FullName
##### BEGIN Tasks Unique to this Module's Build #####
# Build the Get-PUDAdminCenter Public Function
$PUDAppMainFunctionTemplateContent = Get-Content "$env:BHModulePath\Private\PUDAppMainFunctionTemplate.ps1"
$DynamicPagesContent = foreach ($FileItem in @(Get-ChildItem -Path "$env:BHModulePath\Pages\Dynamic" -File)) {
@(
Get-Content $FileItem.FullName
""
)
}
$StaticPagesContent = foreach ($FileItem in @(Get-ChildItem -Path "$env:BHModulePath\Pages\Static" -File)) {
@(
Get-Content $FileItem.FullName
""
)
}
$GetPUDAdminCenterFunction = $PUDAppMainFunctionTemplateContent | foreach {
if ($_ -eq "'Add Dynamic Pages Here'") {
$DynamicPagesContent | foreach {' ' + $_}
}
elseif ($_ -eq "'Add Static Pages Here'") {
$StaticPagesContent | foreach {' ' + $_}
}
else {
$_
}
}
Set-Content -Path $env:BHModulePath\Public\Get-PUDAdminCenter.ps1 -Value $GetPUDAdminCenterFunction
##### END Tasks Unique to this Module's Build #####
[System.Collections.ArrayList]$FilesFailedToSign = @()
foreach ($FilePath in $FilesToSign.FullName) {
try {
Write-Host "Signing $FilePath..."
$SetAuthenticodeResult = Set-AuthenticodeSignature -FilePath $FilePath -cert $Cert
if (!$SetAuthenticodeResult -or $SetAuthenticodeResult.Status -eq "HasMisMatch") {throw}
}
catch {
Write-Warning "Signing $FilePath failed!"
$null = $FilesFailedToSign.Add($FilePath)
}
}
if ($FilesFailedToSign.Count -gt 0) {
Write-Error "Halting because we failed to digitally sign the following files:`n$($FilesFailedToSign -join "`n")"
$global:FunctionResult = "1"
return
}
}
#endregion >> Sign Everything
if (!$(Get-Module -ListAvailable PSDepend)) {
& $(Resolve-Path "$PSScriptRoot\*Help*\Install-PSDepend.ps1").Path
}
try {
Import-Module PSDepend
$null = Invoke-PSDepend -Path "$PSScriptRoot\build.requirements.psd1" -Install -Import -Force
# Hack to fix AppVeyor Error When attempting to Publish to PSGallery
# The specific error this fixes is a problem with the Publish-Module cmdlet from PowerShellGet. PSDeploy
# calls Publish-Module without the -Force parameter which results in this error: https://github.com/PowerShell/PowerShellGet/issues/79
# This is more a problem with PowerShellGet than PSDeploy.
Remove-Module PSDeploy -ErrorAction SilentlyContinue
$PSDeployScriptToEdit = Get-Childitem -Path $(Get-Module -ListAvailable PSDeploy).ModuleBase -File -Recurse -Filter "PSGalleryModule.ps1"
[System.Collections.ArrayList][array]$PSDeployScriptContent = Get-Content $PSDeployScriptToEdit.FullName
$LineOfInterest = $($PSDeployScriptContent | Select-String -Pattern ".*?Verbose[\s]+= \`$VerbosePreference").Matches.Value
$IndexOfLineOfInterest = $PSDeployScriptContent.IndexOf($LineOfInterest)
$PSDeployScriptContent.Insert($($IndexOfLineOfInterest+1)," Force = `$True")
Set-Content -Path $PSDeployScriptToEdit.FullName -Value $PSDeployScriptContent
Import-Module PSDeploy
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
if ([bool]$(Get-Module -Name $env:BHProjectName -ErrorAction SilentlyContinue)) {
Remove-Module $env:BHProjectName -Force -ErrorAction SilentlyContinue
}
##### BEGIN Tasks Unique to this Module's Build #####
# Build the Get-PUDAdminCenter Public Function
$PUDAppMainFunctionTemplateContent = Get-Content "$env:BHModulePath\Private\PUDAppMainFunctionTemplate.ps1"
$DynamicPagesContent = foreach ($FileItem in @(Get-ChildItem -Path "$env:BHModulePath\Pages\Dynamic" -File)) {
@(
Get-Content $FileItem.FullName
""
)
}
$StaticPagesContent = foreach ($FileItem in @(Get-ChildItem -Path "$env:BHModulePath\Pages\Static" -File)) {
@(
Get-Content $FileItem.FullName
""
)
}
$GetPUDAdminCenterFunction = $PUDAppMainFunctionTemplateContent | foreach {
if ($_ -eq "'Add Dynamic Pages Here'") {
$DynamicPagesContent | foreach {' ' + $_}
}
elseif ($_ -eq "'Add Static Pages Here'") {
$StaticPagesContent | foreach {' ' + $_}
}
else {
$_
}
}
Set-Content -Path $env:BHModulePath\Public\Get-PUDAdminCenter.ps1 -Value $GetPUDAdminCenterFunction
##### END Tasks Unique to this Module's Build #####
$psakeFile = "$env:BHProjectPath\psake.ps1"
if (!$(Test-Path $psakeFile)) {
Write-Error "Unable to find the path $psakeFile! Halting!"
$global:FunctionResult = "1"
return
}
if ($PSBoundParameters.ContainsKey('help')) {
Get-PSakeScriptTasks -buildFile $psakeFile | Format-Table -Property Name, Description, Alias, DependsOn
return
}
##### END Prepare For Build #####
##### BEGIN PSAKE Build #####
# Add any test resources that you want to push to psake.ps1 and/or *.Tests.ps1 files
$TestResources = @{}
$InvokePSakeParams = @{}
if ($Cert) {
$InvokePSakeParams.Add("Cert",$Cert)
}
if ($TestResources.Count -gt 0) {
$InvokePSakeParams.Add("TestResources",$TestResources)
}
if ($InvokePSakeParams.Count -gt 0) {
Invoke-Psake $psakeFile -taskList $Task -nologo -parameters $InvokePSakeParams -ErrorVariable IPSErr
}
else {
Invoke-Psake $psakeFile -taskList $Task -nologo -ErrorAction Stop
}
# If we're NOT in AppVeyor, we want to sign everything again
if ($env:BHBuildSystem -ne 'AppVeyor') {
#region >> Sign Everything
if ($CertFileForSignature -and !$Cert) {
if (!$(Test-Path $CertFileForSignature)) {
Write-Error "Unable to find the Certificate specified to be used for Code Signing! Halting!"
$global:FunctionResult = "1"
return
}
try {
$Cert = Get-PfxCertificate $CertFileForSignature -ErrorAction Stop
if (!$Cert) {throw "There was a prblem with the Get-PfcCertificate cmdlet! Halting!"}
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
}
if ($Cert) {
# Make sure the Cert is good for Code Signing
if ($Cert.EnhancedKeyUsageList.ObjectId -notcontains "1.3.6.1.5.5.7.3.3") {
$CNOfCert = $($($Cert.Subject -split ",")[0] -replace "CN=","").Trim()
Write-Error "The provided Certificate $CNOfCert says that it should be sued for $($Cert.EnhancedKeyUsageList.FriendlyName -join ','), NOT 'Code Signing'! Halting!"
$global:FunctionResult = "1"
return
}
# Make sure our ProtoHelpers are signed before we do anything else, otherwise we won't be able to use them
$HelperFilestoSign = Get-ChildItem $(Resolve-Path "$PSScriptRoot\*Help*\").Path -Recurse -File | Where-Object {
$_.Extension -match '\.ps1|\.psm1|\.psd1|\.ps1xml' -and $_.Name -ne "Remove-Signature.ps1"
}
# Before we loop through and sign the Helper functions, we need to sign Remove-Signature.ps1
$RemoveSignatureFilePath = $(Resolve-Path "$PSScriptRoot\*Help*\Remove-Signature.ps1").Path
if (!$(Test-Path $RemoveSignatureFilePath)) {
Write-Error "Unable to find the path $RemoveSignatureFilePath! Halting!"
$global:FunctionResult = "1"
return
}
# Because Set-Authenticode sometimes eats a trailing line when it is used, make sure Remove-Signature.ps1 doesn't break
$SingatureLineRegex = '^# SIG # Begin signature block|^<!-- SIG # Begin signature block -->'
$RemoveSignatureContent = Get-Content $RemoveSignatureFilePath
[System.Collections.ArrayList]$UpdatedRemoveSignatureContent = @()
foreach ($line in $RemoveSignatureContent) {
if ($line -match $SingatureLineRegex) {
$null = $UpdatedRemoveSignatureContent.Add("`n")
break
}
else {
$null = $UpdatedRemoveSignatureContent.Add($line)
}
}
Set-Content -Path $RemoveSignatureFilePath -Value $UpdatedRemoveSignatureContent
try {
$SetAuthenticodeResult = Set-AuthenticodeSignature -FilePath $RemoveSignatureFilePath -Cert $Cert
if (!$SetAuthenticodeResult -or $SetAuthenticodeResult.Status -ne "Valid") {throw "There was a problem using the Set-AuthenticodeSignature cmdlet to sign the Remove-Signature.ps1 function! Halting!"}
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
# Dot Source the Remove-Signature function
. $RemoveSignatureFilePath
if (![bool]$(Get-Item Function:\Remove-Signature)) {
Write-Error "Problem dot sourcing the Remove-Signature function! Halting!"
$global:FunctionResult = "1"
return
}
# Loop through the Help Scripts/Functions and sign them so that we can use them immediately if necessary
Remove-Signature -FilePath $HelperFilestoSign.FullName
[System.Collections.ArrayList]$FilesFailedToSign = @()
foreach ($FilePath in $HelperFilestoSign.FullName) {
try {
Write-Host "Signing $FilePath..."
$SetAuthenticodeResult = Set-AuthenticodeSignature -FilePath $FilePath -cert $Cert
if (!$SetAuthenticodeResult -or $SetAuthenticodeResult.Status -ne "Valid") {throw}
}
catch {
Write-Warning "Signing $FilePath failed!"
$null = $FilesFailedToSign.Add($FilePath)
}
}
if ($FilesFailedToSign.Count -gt 0) {
Write-Error "Halting because we failed to digitally sign the following files:`n$($FilesFailedToSign -join "`n")"
$global:FunctionResult = "1"
return
}
}
if ($Cert) {
# NOTE: We don't want to include the Module's .psm1 or .psd1 yet because the psake.ps1 Compile Task hasn't finalized them yet...
# NOTE: We don't want to sign build.ps1, Remove-Signature.ps1, or Helper functions because we just did that above...
$HelperFilesToSignNameRegex = $HelperFilestoSign.Name | foreach {[regex]::Escape($_)}
$RemoveSignatureFilePathRegex = [regex]::Escape($RemoveSignatureFilePath)
[System.Collections.ArrayList][array]$FilesToSign = Get-ChildItem $env:BHProjectPath -Recurse -File | Where-Object {
$_.Extension -match '\.ps1|\.psm1|\.psd1|\.ps1xml' -and
$_.Name -notmatch "^module\.requirements\.psd1" -and
$_.Name -notmatch "^build\.requirements\.psd1" -and
$_.Name -notmatch "^build\.ps1$" -and
$_.Name -notmatch $($HelperFilesToSignNameRegex -join '|') -and
$_.Name -notmatch $RemoveSignatureFilePathRegex -and
$_.FullName -notmatch "\\Pages\\Dynamic|\\Pages\\Static"
}
#$null = $FilesToSign.Add($(Get-Item $env:BHModulePath\Install-PSDepend.ps1))
Remove-Signature -FilePath $FilesToSign.FullName
##### BEGIN Tasks Unique to this Module's Build #####
# Build the Get-PUDAdminCenter Public Function
$PUDAppMainFunctionTemplateContent = Get-Content "$env:BHModulePath\Private\PUDAppMainFunctionTemplate.ps1"
$DynamicPagesContent = foreach ($FileItem in @(Get-ChildItem -Path "$env:BHModulePath\Pages\Dynamic" -File)) {
@(
Get-Content $FileItem.FullName
""
)
}
$StaticPagesContent = foreach ($FileItem in @(Get-ChildItem -Path "$env:BHModulePath\Pages\Static" -File)) {
@(
Get-Content $FileItem.FullName
""
)
}
$GetPUDAdminCenterFunction = $PUDAppMainFunctionTemplateContent | foreach {
if ($_ -eq "'Add Dynamic Pages Here'") {
$DynamicPagesContent | foreach {' ' + $_}
}
elseif ($_ -eq "'Add Static Pages Here'") {
$StaticPagesContent | foreach {' ' + $_}
}
else {
$_
}
}
Set-Content -Path $env:BHModulePath\Public\Get-PUDAdminCenter.ps1 -Value $GetPUDAdminCenterFunction
##### END Tasks Unique to this Module's Build #####
[System.Collections.ArrayList]$FilesFailedToSign = @()
foreach ($FilePath in $FilesToSign.FullName) {
try {
Write-Host "Signing $FilePath..."
$SetAuthenticodeResult = Set-AuthenticodeSignature -FilePath $FilePath -cert $Cert
if (!$SetAuthenticodeResult -or $SetAuthenticodeResult.Status -eq "HasMisMatch") {throw}
}
catch {
Write-Warning "Signing $FilePath failed!"
$null = $FilesFailedToSign.Add($FilePath)
}
}
if ($FilesFailedToSign.Count -gt 0) {
Write-Error "Halting because we failed to digitally sign the following files:`n$($FilesFailedToSign -join "`n")"
$global:FunctionResult = "1"
return
}
}
#endregion >> Sign Everything
}
exit ( [int]( -not $psake.build_success ) )
##### END PSAKE Build #####
# SIG # Begin signature block
# MIIMiAYJKoZIhvcNAQcCoIIMeTCCDHUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU2E+LZvpuVl6F3XEdD9wtekwN
# eRSgggn9MIIEJjCCAw6gAwIBAgITawAAAB/Nnq77QGja+wAAAAAAHzANBgkqhkiG
# 9w0BAQsFADAwMQwwCgYDVQQGEwNMQUIxDTALBgNVBAoTBFpFUk8xETAPBgNVBAMT
# CFplcm9EQzAxMB4XDTE3MDkyMDIxMDM1OFoXDTE5MDkyMDIxMTM1OFowPTETMBEG
# CgmSJomT8ixkARkWA0xBQjEUMBIGCgmSJomT8ixkARkWBFpFUk8xEDAOBgNVBAMT
# B1plcm9TQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDCwqv+ROc1
# bpJmKx+8rPUUfT3kPSUYeDxY8GXU2RrWcL5TSZ6AVJsvNpj+7d94OEmPZate7h4d
# gJnhCSyh2/3v0BHBdgPzLcveLpxPiSWpTnqSWlLUW2NMFRRojZRscdA+e+9QotOB
# aZmnLDrlePQe5W7S1CxbVu+W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