1
+ <#
2
+ Check (and recurse) current directory .\CheckSignature.ps1
3
+ Check directory after MSI install .\CheckSignature.ps1 -MsiInstall
4
+ Check directory after gallery install .\CheckSignature.ps1 -GalleryInstall
5
+ #>
6
+ [CmdletBinding (DefaultParameterSetName = " CurrentDirectory" )]
7
+ Param
8
+ (
9
+ [Parameter (ParameterSetName = " MsiInstall" , Mandatory = $true )]
10
+ [switch ]$MsiInstall ,
11
+ [Parameter (ParameterSetName = " GalleryInstall" , Mandatory = $true )]
12
+ [switch ]$GalleryInstall ,
13
+ [Parameter (ParameterSetName = " CustomPath" , Mandatory = $true )]
14
+ [string ]$CustomPath
15
+ )
16
+
17
+ function Check-StrongName {
18
+ [CmdletBinding ()]
19
+ param ([Parameter (ValueFromPipeline = $true )][string ]$path )
20
+ $output = & " sn.exe" - vf $path
21
+ $length = $output.Length - 1
22
+ if (-not $output [$length ].Contains(" is valid" )) {
23
+ Write-Output " $path has an invalid strong name."
24
+ }
25
+ }
26
+
27
+ function Check-AuthenticodeSignature {
28
+ [CmdletBinding ()]
29
+ param ([Parameter (ValueFromPipeline = $true )][string ]$path )
30
+ $output = Get-AuthenticodeSignature $path
31
+ if (-not ($output.Status -like " Valid" )) {
32
+ Write-Output " $path has an invalid authenticode signature. Status is $ ( $output.Status ) "
33
+ }
34
+ }
35
+
36
+ function Check-All {
37
+ [CmdletBinding ()]
38
+ param ([Parameter ()][string ]$path )
39
+
40
+ $invalidList = @ ()
41
+
42
+ $files = Get-ChildItem $path \* - Include * .dll - Recurse | where { $_.FullName -like " *Azure*" }
43
+ Write-Host " Checking the strong name signature of $ ( $files.Count ) files (.dll)" - ForegroundColor Yellow
44
+
45
+ $invalidStrongNameList = @ ()
46
+
47
+ for ($idx = 0 ; $idx -lt $files.Length ; $idx ++ ) {
48
+ $percent = (100 * $idx ) / $files.Length
49
+ Write-Progress - Activity " Validating strong name signature of $ ( $files [$idx ]) " - Status " $percent % Complete" - PercentComplete $percent
50
+ $invalidStrongNameList += Check- StrongName - path $files [$idx ]
51
+ }
52
+
53
+ if ($invalidStrongNameList.Length -gt 0 ) {
54
+ $invalidList += $invalidStrongNameList
55
+ Write-Host " Found $ ( $invalidStrongNameList.Count ) files with an invalid strong name signature." - ForegroundColor Red
56
+ }
57
+ else {
58
+ Write-Host " All files (.dll) have a strong name signature." - ForegroundColor Green
59
+ }
60
+
61
+ # -------------------------------------
62
+
63
+ $files = Get-ChildItem $path \* - Include * .dll, * .ps1, * .psm1 - Recurse | where { $_.FullName -like " *Azure*" }
64
+ $files = $files | where { ($_.FullName -notlike " *Newtonsoft.Json*" ) -and `
65
+ ($_.FullName -notlike " *AutoMapper*" ) -and `
66
+ ($_.FullName -notlike " *Security.Cryptography*" ) -and `
67
+ ($_.FullName -notlike " *BouncyCastle.Crypto*" )}
68
+ Write-Host " Checking the authenticode signature of $ ( $files.Count ) files (.dll, .ps1, .psm1)" - ForegroundColor Yellow
69
+
70
+ $invalidAuthenticodeList = @ ()
71
+
72
+ for ($idx = 0 ; $idx -lt $files.Length ; $idx ++ ) {
73
+ $percent = (100 * $idx ) / $files.Length
74
+ Write-Progress - Activity " Validating authenticode signature of $ ( $files [$idx ]) " - Status " $percent % Complete" - PercentComplete $percent
75
+ $invalidAuthenticodeList += Check- AuthenticodeSignature - path $files [$idx ]
76
+ }
77
+
78
+ if ($invalidAuthenticodeList.Length -gt 0 ) {
79
+ $invalidList += $invalidAuthenticodeList
80
+ Write-Host " Found $ ( $invalidAuthenticodeList.Count ) files with an invalid authenticode signature." - ForegroundColor Red
81
+ }
82
+ else {
83
+ Write-Host " All files (.dll, .ps1, .psd1) have a valid authenticode signature." - ForegroundColor Green
84
+ }
85
+
86
+ if ($invalidList.Length -gt 0 ) {
87
+ Write-Output ($invalidList )
88
+ throw " Strong name signature check and/or authenticode signature check failed. Please see the above errors."
89
+ }
90
+ }
91
+
92
+ $path = " .\"
93
+
94
+ if ($PSCmdlet.ParameterSetName -eq " MsiInstall" )
95
+ {
96
+ $path = " ${env: ProgramFiles(x86)} \Microsoft SDKs\Azure\PowerShell"
97
+ Write-Host " Installed Azure PowerShell from MSI - checking all (Azure) files in $path " - ForegroundColor Yellow
98
+ }
99
+ elseif ($PSCmdlet.ParameterSetName -eq " GalleryInstall" )
100
+ {
101
+ $path = " $ ( $env: ProgramFiles ) \WindowsPowerShell\Modules"
102
+ Write-Host " Installed Azure PowerShell from the gallery - checking all (Azure) files in $path " - ForegroundColor Yellow
103
+ }
104
+ elseif ($PSCmdlet.ParameterSetName -eq " CustomPath" )
105
+ {
106
+ $path = $CustomPath
107
+ Write-Host " Custom path provided - checking all (Azure) files in $path " - ForegroundColor Yellow
108
+ }
109
+ else
110
+ {
111
+ Write-Host " No switch parameter set - checking all files in current directory" - ForegroundColor Yellow
112
+ }
113
+
114
+ Check- All $path
0 commit comments