forked from jhochwald/PowerShell-collection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSignAllFiles.ps1
119 lines (100 loc) · 2.85 KB
/
SignAllFiles.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
<#
.SYNOPSIS
Sign all scripts of the PowerShell-collection project
.DESCRIPTION
Sign all scripts of the PowerShell-collection project with the default certificate. We import the complete chain.
.EXAMPLE
PS C:\> .\SignAllFiles.ps1
Sign all scripts of the PowerShell-collection project
.EXAMPLE
PS C:\> .\SignAllFiles.ps1 -verbose
Sign all scripts of the PowerShell-collection project with verbose parameter
.NOTES
We import the complete certificate chain. and use a Timestamp Server
#>
[CmdletBinding(ConfirmImpact = 'None')]
param ()
begin
{
try
{
Write-Verbose -Message 'Get the code signing certificate'
$paramGetChildItem = @{
Path = 'cert:\CurrentUser\My'
CodeSigningCert = $true
ErrorAction = 'Stop'
WarningAction = 'SilentlyContinue'
}
$Cert = (Get-ChildItem @paramGetChildItem)[0]
Write-Verbose -Message ('We found the following certificate: {0}' -f $Cert)
}
catch
{
$paramWriteError = @{
Message = 'No Code Signing Certificate was found!'
Category = 'ObjectNotFound'
TargetObject = 'CodeSigningCert'
RecommendedAction = 'Check your certificate store.'
ErrorAction = 'Stop'
}
Write-Error @paramWriteError
break
}
}
process
{
$BaseDirs = 'Misc', 'Exchange', 'ActiveDirectory', 'Office_Related', 'ExchangeOnline', 'WSUS', 'Office365', 'Skype_for_Business', 'Skype_for_Business\rms4bcert'
foreach ($BaseDir in $BaseDirs)
{
$SignDir = 'Y:\dev\Clones\new\PowerShell-collection\' + $BaseDir + '\signed\*.ps1'
Write-Verbose -Message ('Processing: {0}' -f $SignDir)
try
{
$AllFiles = $null
$paramGetChildItem = @{
Path = $SignDir
ErrorAction = 'Stop'
WarningAction = 'SilentlyContinue'
}
$AllFiles = (Get-ChildItem @paramGetChildItem)
}
catch
{
$AllFiles = $null
}
if ($AllFiles)
{
foreach ($item in $AllFiles)
{
try
{
Write-Verbose -Message ('Signing {0}' -f $item)
$paramSetAuthenticodeSignature = @{
FilePath = $item
Certificate = $Cert
IncludeChain = 'All'
TimestampServer = 'http://timestamp.digicert.com'
Force = $true
Confirm = $false
ErrorAction = 'Stop'
WarningAction = 'SilentlyContinue'
}
$null = (Set-AuthenticodeSignature @paramSetAuthenticodeSignature)
Write-Verbose -Message ('Signed {0}' -f $item)
}
catch
{
Write-Warning -Message ('Unable to Sign {0}' -f $item)
}
}
}
else
{
Write-Warning -Message ('Sorry {0} caused issues...' -f $SignDir)
}
}
}
end
{
Write-Verbose -Message 'We are done'
}