|
1 | 1 | # ------------------------------------------------------------------------------ |
2 | 2 | # Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. |
3 | | -# ------------------------------------------------------------------------------ |
| 3 | +# |
4 | 4 |
|
5 | | -[cmdletbinding()] |
| 5 | +[CmdletBinding()] |
6 | 6 | param( |
7 | | - [ArgumentCompleter({ (Get-ChildItem -Path "$PSScriptRoot\..\module").Name })] |
8 | | - [string] |
9 | | - $ModuleName = 'Entra', |
| 7 | + # The name of the module to install dependencies for. The module name should match the folder name in the module folder. If not provided, the script will default to 'Entra'. |
| 8 | + [ArgumentCompleter({ (Get-ChildItem -Path "$PSScriptRoot\..\module").Name })] |
| 9 | + [string] |
| 10 | + $ModuleName = 'Entra', |
10 | 11 |
|
11 | | - [ValidateScript({ Test-Path $_ })] |
12 | | - [string] |
13 | | - $ModuleSettingsPath, |
| 12 | + # Path to the ModuleSettings.json file. If not provided, the script will look for the file in the module folder. |
| 13 | + [ValidateScript({ Test-Path $_ })] |
| 14 | + [string] |
| 15 | + $ModuleSettingsPath, |
14 | 16 |
|
15 | | - # Force installation of required modules, even if they are already installed. |
16 | | - [switch] |
17 | | - $Force |
| 17 | + # Force installation of the required modules, even if they are already installed. |
| 18 | + [switch] |
| 19 | + $Force |
18 | 20 | ) |
19 | 21 |
|
20 | | -. "$psscriptroot/common-functions.ps1" |
| 22 | +try { |
| 23 | + # Review: are the common functions required for this script? There does not seem to be any dependencies on them [yet?]. |
| 24 | + . "$PSScriptRoot/common-functions.ps1" |
| 25 | +} catch { |
| 26 | + Write-Error -Message "Failed to load common-functions.ps1. Error: $_" -RecommendedAction "This script should be run from the 'build' folder. Ensure 'common-functions.ps1' exists and is accessible." |
| 27 | +} |
21 | 28 |
|
22 | | -$settingPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json" |
23 | | -if ($ModuleSettingsPath) { $settingPath = $ModuleSettingsPath } |
24 | | -$content = Get-Content -Path $settingPath | ConvertFrom-Json |
| 29 | +if ($ModuleSettingsPath) { |
| 30 | + $SettingsPath = $ModuleSettingsPath |
| 31 | +} else { |
| 32 | + $SettingsPath = "$PSScriptRoot/../module/$ModuleName/config/ModuleSettings.json" |
| 33 | +} |
| 34 | +$ModuleSettings = Get-Content -Path $SettingsPath | ConvertFrom-Json |
| 35 | +$RequiredVersion = $ModuleSettings.destinationModuleVersion |
25 | 36 |
|
26 | 37 | if ($Force) { |
27 | | - Write-Verbose 'Skipping the check for installed prerequisites.' |
| 38 | + Write-Verbose 'Skipping the check for installed prerequisites. Forcing the installation of all required modules.' |
28 | 39 | } else { |
29 | | - Write-Verbose 'Checking installed modules for required dependencies.' |
30 | | - $InstalledModules = Get-Module -ListAvailable -Verbose:$false | Group-Object -Property Name |
| 40 | + Write-Verbose 'Checking installed modules for required dependencies.' |
| 41 | + $InstalledModules = Get-Module -ListAvailable -Verbose:$false | Group-Object -Property Name |
31 | 42 | } |
32 | 43 |
|
33 | | -$SourceModule = $content.sourceModule |
| 44 | +# Install the AzureAD module. |
| 45 | +$SourceModule = $ModuleSettings.sourceModule |
34 | 46 | if (($InstalledModules.Name -contains $SourceModule) -and -not $Force) { |
35 | | - Write-Verbose "The $SourceModule module is already installed." |
| 47 | + Write-Verbose "The $SourceModule module is already installed." |
36 | 48 | } else { |
37 | | - Write-Verbose("Installing Module: $sourceModule") |
38 | | - Install-Module $sourceModule -Scope CurrentUser -Force -AllowClobber |
| 49 | + Write-Verbose("Installing Module: $sourceModule") |
| 50 | + try { |
| 51 | + Install-Module $sourceModule -Scope CurrentUser -Force -AllowClobber |
| 52 | + } catch { |
| 53 | + Write-Error "Failed to install the $sourceModule module. Error: $_" |
| 54 | + } |
39 | 55 | } |
40 | 56 |
|
41 | | -foreach ($moduleName in $content.destinationModuleName) { |
42 | | - $InstalledModuleReference = $InstalledModules.Where({ $_.Name -eq $moduleName }).Group |
43 | | - if (($InstalledModuleReference.Version -ge $content.destinationModuleVersion) -and -not $Force) { |
44 | | - Write-Verbose "The $moduleName module is already installed." |
45 | | - } else { |
46 | | - Write-Verbose "Installing Module: $moduleName" |
47 | | - Install-Module $moduleName -Scope CurrentUser -RequiredVersion $content.destinationModuleVersion -Force -AllowClobber |
48 | | - } |
| 57 | +# Install the required module dependencies if missing the required version or if -Force. |
| 58 | +foreach ($moduleName in $ModuleSettings.destinationModuleName) { |
| 59 | + $InstalledModuleReference = $InstalledModules.Where({ $_.Name -eq $moduleName }) |
| 60 | + if (-not $InstalledModuleReference -or $Force) { |
| 61 | + Write-Verbose "Installing version $RequiredVersion of $moduleName" |
| 62 | + try { |
| 63 | + Install-Module $moduleName -Scope CurrentUser -RequiredVersion $RequiredVersion -Force -AllowClobber |
| 64 | + } catch { |
| 65 | + Write-Error "Failed to install module $moduleName ${RequiredVersion}. Error: $_" |
| 66 | + } |
| 67 | + } elseif ($InstalledModuleReference.Group.Version -contains $RequiredVersion) { |
| 68 | + Write-Verbose "Found version $RequiredVersion of $moduleName" |
| 69 | + } |
49 | 70 | } |
0 commit comments