Skip to content

Commit 4bbc7a2

Browse files
committed
Resolve review comments, add more error checks, add -AzurePowerShell switch to find and check modules
1 parent c08a421 commit 4bbc7a2

File tree

1 file changed

+110
-12
lines changed

1 file changed

+110
-12
lines changed

tools/CheckSignature.ps1

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<#
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
2+
Check (and recurse) current directory .\CheckSignature.ps1
3+
Find and check directory of Azure PowerShell modules .\CheckSignature.ps1 -AzurePowerShell
4+
Check directory of MSI-installed modules .\CheckSignature.ps1 -MsiInstall
5+
Check directory of gallery-installed modules .\CheckSignature.ps1 -GalleryInstall
6+
Check files in provided path .\CheckSignature.ps1 -CustomPath $Path
57
#>
68
[CmdletBinding(DefaultParameterSetName="CurrentDirectory")]
79
Param
810
(
11+
[Parameter(ParameterSetName="AzurePowerShell", Mandatory=$true)]
12+
[switch]$AzurePowerShell,
913
[Parameter(ParameterSetName="MsiInstall", Mandatory=$true)]
1014
[switch]$MsiInstall,
1115
[Parameter(ParameterSetName="GalleryInstall", Mandatory=$true)]
@@ -37,9 +41,15 @@ function Check-All {
3741
[CmdletBinding()]
3842
param([Parameter()][string]$path)
3943

44+
if (!(Get-Command "sn.exe" -ErrorAction SilentlyContinue))
45+
{
46+
Write-Error "Unable to find sn.exe; please ensure that the Windows SDK is installed and found in the PATH environment variable."
47+
return
48+
}
49+
4050
$invalidList = @()
4151

42-
$files = Get-ChildItem $path\* -Include *.dll -Recurse | where { $_.FullName -like "*Azure*" }
52+
$files = Get-ChildItem $path\* -Include *.dll -Recurse | Where-Object { $_.FullName -like "*Azure*" }
4353
Write-Host "Checking the strong name signature of $($files.Count) files (.dll)" -ForegroundColor Yellow
4454

4555
$invalidStrongNameList = @()
@@ -60,11 +70,11 @@ function Check-All {
6070

6171
# -------------------------------------
6272

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*")}
73+
$files = Get-ChildItem $path\* -Include *.dll, *.ps1, *.psm1 -Recurse | Where-Object { $_.FullName -like "*Azure*" }
74+
$files = $files | Where-Object { ($_.FullName -notlike "*Newtonsoft.Json*") -and `
75+
($_.FullName -notlike "*AutoMapper*") -and `
76+
($_.FullName -notlike "*Security.Cryptography*") -and `
77+
($_.FullName -notlike "*BouncyCastle.Crypto*")}
6878
Write-Host "Checking the authenticode signature of $($files.Count) files (.dll, .ps1, .psm1)" -ForegroundColor Yellow
6979

7080
$invalidAuthenticodeList = @()
@@ -91,14 +101,102 @@ function Check-All {
91101

92102
$path = ".\"
93103

94-
if ($PSCmdlet.ParameterSetName -eq "MsiInstall")
104+
if ($PSCmdlet.ParameterSetName -eq "AzurePowerShell")
95105
{
96-
$path = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell"
106+
$ProfileModule = Get-Module -Name AzureRM.Profile -ListAvailable
107+
if (!($ProfileModule))
108+
{
109+
Write-Error "Unable to find the AzureRM.Profile module. Please ensure that Azure PowerShell has been installed and the appropriate path is found in the PSModulePath environment variable."
110+
return
111+
}
112+
113+
if ($ProfileModule.Count -gt 1)
114+
{
115+
Write-Error "Mulitple versions of Azure PowerShell were found. Please use the -MsiInstall and -GalleryInstall switches to select the installed version you want to check."
116+
return
117+
}
118+
119+
$ModulePath = $ProfileModule.Path
120+
if ($ModulePath -like "*Microsoft SDKs\Azure\PowerShell*")
121+
{
122+
$EndIdx = $ModulePath.IndexOf("PowerShell\ResourceManager", [System.StringComparison]::OrdinalIgnoreCase) + "PowerShell".Length
123+
}
124+
elseif ($ModulePath -like "*Modules\AzureRM.Profile*")
125+
{
126+
$EndIdx = $ModulePath.IndexOf("Modules\AzureRM.Profile", [System.StringComparison]::OrdinalIgnoreCase) + "Modules".Length
127+
}
128+
129+
$path = $ModulePath.Substring(0, $EndIdx)
130+
Write-Host "Found AzureRM module - checking all (Azure) files in $path" -ForegroundColor Yellow
131+
}
132+
elseif ($PSCmdlet.ParameterSetName -eq "MsiInstall")
133+
{
134+
$ProfileModule = Get-Module -Name AzureRM.Profile -ListAvailable
135+
if (!($ProfileModule))
136+
{
137+
Write-Error "Unable to find the AzureRM.Profile module. Please ensure that Azure PowerShell has been installed and the appropriate path is found in the PSModulePath environment variable."
138+
return
139+
}
140+
141+
$SearchString = "Microsoft SDKs\Azure\PowerShell"
142+
143+
$ModulePath = $ProfileModule.Path
144+
145+
if ($ProfileModule.Count -gt 1)
146+
{
147+
$ModulePath = $ProfileModule | Where-Object { $_.Path -like "*$($SearchString)*" }
148+
if (!($ModulePath))
149+
{
150+
Write-Error "Unable to find path of MSI-installed modules from multiple locations found in PSModulePath."
151+
return
152+
}
153+
}
154+
else
155+
{
156+
if ($ModulePath -notlike "*$($SearchString)*")
157+
{
158+
Write-Error "Modules installed on the current machine were not from MSI. Consider using the -GalleryInstall switch."
159+
return
160+
}
161+
}
162+
163+
$EndIdx = $ModulePath.IndexOf("PowerShell\ResourceManager", [System.StringComparison]::OrdinalIgnoreCase) + "PowerShell".Length
164+
$path = $ModulePath.Substring(0, $EndIdx)
97165
Write-Host "Installed Azure PowerShell from MSI - checking all (Azure) files in $path" -ForegroundColor Yellow
98166
}
99167
elseif ($PSCmdlet.ParameterSetName -eq "GalleryInstall")
100168
{
101-
$path = "$($env:ProgramFiles)\WindowsPowerShell\Modules"
169+
$ProfileModule = Get-Module -Name AzureRM.Profile -ListAvailable
170+
if (!($ProfileModule))
171+
{
172+
Write-Error "Unable to find the AzureRM.Profile module. Please ensure that Azure PowerShell has been installed and the appropriate path is found in the PSModulePath environment variable."
173+
return
174+
}
175+
176+
$SearchString = "WindowsPowerShell\Modules"
177+
178+
$ModulePath = $ProfileModule.Path
179+
180+
if ($ProfileModule.Count -gt 1)
181+
{
182+
$ModulePath = $ProfileModule | Where-Object { $_.Path -like "*$($SearchString)*" }
183+
if (!($ModulePath))
184+
{
185+
Write-Error "Unable to find path of gallery-installed modules from multiple locations found in PSModulePath."
186+
return
187+
}
188+
}
189+
else
190+
{
191+
if ($ModulePath -notlike "*$($SearchString)*")
192+
{
193+
Write-Error "Modules installed on the current machine were not from the gallery. Consider using the -MsiInstall switch."
194+
return
195+
}
196+
}
197+
198+
$EndIdx = $ModulePath.IndexOf("Modules\AzureRM.Profile", [System.StringComparison]::OrdinalIgnoreCase) + "Modules".Length
199+
$path = $ModulePath.Substring(0, $EndIdx)
102200
Write-Host "Installed Azure PowerShell from the gallery - checking all (Azure) files in $path" -ForegroundColor Yellow
103201
}
104202
elseif ($PSCmdlet.ParameterSetName -eq "CustomPath")

0 commit comments

Comments
 (0)