-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathInitialize-Dependencies.ps1
More file actions
278 lines (246 loc) · 13.1 KB
/
Initialize-Dependencies.ps1
File metadata and controls
278 lines (246 loc) · 13.1 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
param (
[Parameter()]
[string]
$ModuleManifestPath,
[Parameter()]
[string]
$RequiredModulesPath,
[Parameter()]
[switch]
$SkipModuleInstallation,
[Parameter()]
[switch]
$SkipLoadMSAL,
[Parameter()]
[switch]
$DoNotUpdatePSModulePath
)
# Initialize-Dependencies.ps1
# This script is run by the module manifest (ScriptsToProcess) before the module is imported.
# It ensures that dependencies are loaded in the correct order to avoid DLL conflicts.
# Specifically, ExchangeOnlineManagement and Az.Accounts/Graph both use Microsoft.Identity.Client.dll.
# It also updates the PSModulePath to include the directory where dependencies are saved,
# and saves required modules if they are not already present.
function Initialize-Dependencies {
[CmdletBinding()]
param (
[Parameter()]
[string]
$ModuleManifestPath = (Join-Path -Path $PSScriptRoot -ChildPath "ZeroTrustAssessment.psd1" -Resolve -ErrorAction Stop),
[Parameter()]
[string]
$RequiredModulesPath = $(
if ($IsLinux -or $IsMacOS) {
Join-Path -Path $env:HOME -ChildPath ".cache/ZeroTrustAssessment/Modules"
}
else {
Join-Path -Path $env:APPDATA -ChildPath "ZeroTrustAssessment\Modules"
}
),
[Parameter()]
[switch]
$AllowPrerelease,
[Parameter()]
[switch]
$SkipModuleInstallation,
[Parameter()]
[switch]
$SkipLoadMSAL= ($Env:SkipLoadMSAL -eq 'true' -or $Env:SkipLoadMSAL -eq '1'),
[Parameter()]
[switch]
$DoNotUpdatePSModulePath
)
Write-Verbose -Message "=== Initialize-Dependencies.ps1 Starting ==="
#region Prepend $Env:PSModulePath with ~\AppData|.cache\ZeroTrustAssessment\Modules
Write-Verbose -Message ("Setting $Env:PSModulePath to include the {0} for module dependencies..." -f $RequiredModulesPath)
if (-not (Test-Path -Path $RequiredModulesPath -PathType Container))
{
Write-Verbose -Message "RequiredModulesPath path not found: $RequiredModulesPath"
$RequiredModulesPath = (New-Item -Path $RequiredModulesPath -ItemType Directory -Force -ErrorAction Stop).FullName
Write-Verbose -Message "Created output directory: $RequiredModulesPath"
}
else
{
$RequiredModulesPath = (Get-Item -Path $RequiredModulesPath -ErrorAction Stop).FullName
Write-Verbose -Message "RequiredModulesPath path already exists: $RequiredModulesPath"
}
if (-not $DoNotUpdatePSModulePath.IsPresent)
{
Write-Verbose -Message "Updating PSModulePath to include $RequiredModulesPath..."
$modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator).ForEach{$_.TrimEnd([io.path]::DirectorySeparatorChar)}
if ($RequiredModulesPath.TrimEnd([io.path]::DirectorySeparatorChar) -notin $modulePath)
{
Write-Debug -Message "Adding $RequiredModulesPath to PSModulePath."
$env:PSModulePath = (@($RequiredModulesPath) + $modulePath) -join [System.IO.Path]::PathSeparator
Write-Debug -Message "Updated PSModulePath to:`r`n$env:PSModulePath"
}
else
{
Write-Debug -Message "PSModulePath already contains $RequiredModulesPath. No update needed."
}
}
#endregion
#region Load module Manifest
$moduleManifest = Import-PowerShellDataFile -Path $ModuleManifestPath -ErrorAction Stop
[Microsoft.PowerShell.Commands.ModuleSpecification[]]$requiredModules = $moduleManifest.RequiredModules
[Microsoft.PowerShell.Commands.ModuleSpecification[]]$externalModuleDependencies = $moduleManifest.PrivateData.ExternalModuleDependencies
[Microsoft.PowerShell.Commands.ModuleSpecification[]]$xPlatPowerShellRequiredModules = @(
@{ModuleName = 'Microsoft.Graph.Authentication'; GUID = '883916f2-9184-46ee-b1f8-b6a2fb784cee'; ModuleVersion = '2.35.1'; },
@{ModuleName = 'Microsoft.Graph.Beta.Teams'; GUID = 'e264919d-7ae2-4a89-ba8b-524bd93ddc08'; ModuleVersion = '2.35.1'; },
@{ModuleName = 'Az.Accounts'; GUID = '17a2feff-488b-47f9-8729-e2cec094624c'; ModuleVersion = '4.0.2'; },
@{ModuleName = 'ExchangeOnlineManagement'; GUID = 'b5eced50-afa4-455b-847a-d8fb64140a22'; RequiredVersion = '3.9.0'; }
)
[Microsoft.PowerShell.Commands.ModuleSpecification[]]$windowsPowerShellRequiredModules = @(
@{ModuleName = 'Microsoft.Online.SharePoint.PowerShell'; GUID = 'adedde5f-e77b-4682-ab3d-a4cb4ff79b83'; ModuleVersion = '16.0.26914.12004'; },
@{ModuleName = 'AipService'; GUID = 'e338ccc0-3333-4479-87fe-66382d33782d'; ModuleVersion = '3.0.0.1'; }
)
#region Build list of RequiredModule based on OS
[Microsoft.PowerShell.Commands.ModuleSpecification[]]$allModuleDependencies = $requiredModules + $xPlatPowerShellRequiredModules
if ($IsWindows) {
$allModuleDependencies += $windowsPowerShellRequiredModules.Where({
$_.Name -notin $allModuleDependencies.Name
})
}
[Microsoft.PowerShell.Commands.ModuleSpecification[]]$requiredModuleToSave = $allModuleDependencies.Where{
$_.Name -notin $externalModuleDependencies.Name
}
#endregion
if ($IsMacOS -or $IsLinux)
{
#region The ZeroTrustAssessment report should be run in Windows.
# on Non-windows platform, some pillars won't be available, because some required modules only work on Windows PowerShell.
Write-Host -Object "`r`n" -ForegroundColor Yellow
Write-Host -Object '⚠️ Warning: The ZeroTrustAssessment module is designed to run on Windows, in PowerShell 7.'
Write-Host -Object 'Some pillars require modules that can only run on Windows PowerShell (Windows PowerShell 5.1) with implicit remoting.' -ForegroundColor Yellow
# skipping module installation.
#endregion
}
if (-not $SkipModuleInstallation.IsPresent)
{
Write-Host -Object "`r`n"
Write-Host -Object ('Resolving {0} dependencies...' -f $allModuleDependencies.Count) -ForegroundColor Green
$saveModuleCmdParams = @{
Path = $RequiredModulesPath
}
if ($saveModuleCmd = (Get-Command -Name Save-PSResource -ErrorAction Ignore))
{
$saveModuleCmdParams.Add('TrustRepository', $true)
$saveModuleCmdParams.Add('Prerelease', $AllowPrerelease.IsPresent)
Write-Verbose -Message "Saving required modules using Save-PSResource..."
}
elseif ($saveModuleCmd = (Get-Command -Name Save-Module -ErrorAction Ignore))
{
$saveModuleCmdParams.Add('Force', $true)
$saveModuleCmdParams.Add('AllowPrerelease', $AllowPrerelease.IsPresent)
Write-Verbose -Message "Saving required modules using Save-Module..."
}
else
{
Write-Error -Message 'Neither Save-PSResource nor Save-Module is available. Please install the PowerShellGet module to save required modules.'
return
}
foreach ($moduleSpec in $requiredModuleToSave)
{
Write-Verbose -Message ("Saving module {0} with version {1}..." -f $moduleSpec.Name, $moduleSpec.Version)
$saveModuleCmdParamsClone = $saveModuleCmdParams.Clone()
$isModulePresent = Get-Module -FullyQualifiedName $moduleSpec -ListAvailable -ErrorAction Ignore
if ($isModulePresent)
{
Write-Host -Object (' ✅ Module {0} found (v{1}).' -f $moduleSpec.Name,$isModulePresent[0].Version) -ForegroundColor Green
continue
}
try
{
if ($saveModuleCmd.Name -eq 'Save-PSResource')
{
#TODO: use the find before piping result to Save-PSResource.
$saveModuleCmdParamsClone['Name'] = $moduleSpec.Name
# Save-PSResource uses NuGet version range syntax: https://learn.microsoft.com/en-us/nuget/concepts/package-versioning?tabs=semver20sort#version-ranges
if ($moduleSpec.RequiredVersion) {
# Absolute required version
$saveModuleCmdParamsClone['Version'] = '[{0}]' -f $moduleSpec.RequiredVersion
}
elseif ($moduleSpec.MaximumVersion -and $moduleSpec.Version) {
# Minimum and maximum version (exact range) inclusive
$saveModuleCmdParamsClone['Version'] = '[{0},{1}]' -f $moduleSpec.Version, $moduleSpec.MaximumVersion
}
elseif ($moduleSpec.MaximumVersion) {
# Maximum version inclusive
$saveModuleCmdParamsClone['Version'] = '(,{0}]' -f $moduleSpec.MaximumVersion
}
elseif ($moduleSpec.Version) {
# Minimum version inclusive
$saveModuleCmdParamsClone['Version'] = '[{0}, )' -f $moduleSpec.Version
}
$saveModuleCmdParamsClone['PassThru'] = $true
$savedModule = (& $saveModuleCmd @saveModuleCmdParamsClone).Where({ $_.Name -eq $moduleSpec.Name },1)
Write-Host -Object (' ⬇️ Module {0} v{1} saved successfully.' -f $moduleSpec.Name, $savedModule.Version) -ForegroundColor Green
}
elseif ($saveModuleCmd.Name -eq 'Save-Module')
{
$moduleSpec | &$saveModuleCmd @saveModuleCmdParamsClone
Write-Host -Object (' ⬇️ Module {0} saved successfully.' -f $moduleSpec.Name) -ForegroundColor Green
}
}
catch
{
Write-Host -Object (' ❌ Failed to save module {0}: {1}' -f $moduleSpec.Name, $_) -ForegroundColor Red
}
}
}
try {
# Check if MSAL is already loaded
$loadedAssemblies = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq 'Microsoft.Identity.Client' }
if ($loadedAssemblies) {
Write-Warning -Message "MSAL assembly is already loaded. This may cause DLL conflicts."
Write-Verbose -Message " Loaded versions:"
foreach ($asm in $loadedAssemblies) {
Write-Verbose -Message (" - {0} from {1}" -f $asm.GetName().Version, $asm.Location)
}
Write-Verbose -Message " Recommendation: Restart PowerShell and import ZeroTrustAssessment first."
}
else {
Write-Host -Object "`r`n"
Write-Host -Object 'Asserting MSAL loading order for dependencies...' -ForegroundColor Green
$helperPath = Join-Path -Path $PSScriptRoot -ChildPath "private\utility\Get-ModuleImportOrder.ps1" -Resolve -ErrorAction Stop
. $helperPath
Write-Verbose -Message ('Module with DLLs to load: {0}' -f (([Microsoft.PowerShell.Commands.ModuleSpecification[]]$moduleManifest.RequiredModules).Name -join ', '))
# This method does not necessarily load the right dll (it ignores the load logic from the modules)
$msalToLoadInOrder = Get-ModuleImportOrder -Name $allModuleDependencies.Name
$msalToLoadInOrder.ForEach{
Write-Verbose -Message ('Loading MSAL v{0} for dependency {1} version {2}' -f $_.DLLVersion, $_.Name, $_.ModuleVersion)
if ([System.AppDomain]::CurrentDomain.GetAssemblies().Where{$_.GetName().Name -eq 'Microsoft.Identity.Client'}) {
Write-Verbose -Message ("MSAL v{0} is already loaded, skipping." -f $_.DLLVersion)
}
else
{
Write-Host -Object (' ✅ Loading MSAL v{0} for dependency {1} version {2}' -f $_.DLLVersion, $_.Name, $_.ModuleVersion) -ForegroundColor Green
$null = [System.Reflection.Assembly]::LoadFrom($_.DLLPath)
}
# Load related MSAL broker/interop DLLs and Microsoft.IdentityModel.Abstractions (required by MSAL's WithLogging method)
$msalDir = Split-Path -Path $_.DLLPath
$relatedDllsToLoad = Get-ChildItem -Path $msalDir -File | Where-Object {
($_.Name -like 'Microsoft.Identity.Client*.dll' -and $_.Name -ne 'Microsoft.Identity.Client.dll') -or
$_.Name -eq 'Microsoft.IdentityModel.Abstractions.dll'
}
foreach ($relatedDll in $relatedDllsToLoad)
{
Write-Debug -Message ('Loading related MSAL/IdentityModel assembly {0}' -f $relatedDll.Name)
try
{
$null = [System.Reflection.Assembly]::LoadFrom($relatedDll.FullName)
Write-Host -Object (' ✅ Loaded {0}' -f $relatedDll.Name) -ForegroundColor Green
}
catch
{
Write-Debug -Message ("Failed to load related assembly {0}: {1}" -f $relatedDll.FullName, $_)
}
}
}
}
}
catch {
Write-Warning -Message ("Initialize-Dependencies.ps1 failed: {0}" -f $_)
}
}
Initialize-Dependencies @PSBoundParameters