Skip to content

Commit 39a39a7

Browse files
Merge pull request #6752 from FabienTschanz/feat/improve-install-module
Improve module installation speed
2 parents bb8c22a + 709a211 commit 39a39a7

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* M365DSCReverse
2828
* Added `TenantGuid` entry to the `ConfigurationData.psd1` file during export.
2929
FIXES [#6689](https://github.com/microsoft/Microsoft365DSC/issues/6689)
30+
* M365DSCUtil
31+
* Improved module installation speed for `Update-M365DSCModule`.
3032
* MISC
3133
* Removed verbose output from `Get-TargetResource`.
3234
* Updated the error behavior to always throw inside `Get-TargetResource`.

Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ $Global:SessionSecurityCompliance = $null
66
$Script:M365DSCWorkloads = @('AAD', 'ADO', 'AZURE', 'COMMERCE', 'DEFENDER', 'EXO', 'FABRIC', 'INTUNE', 'O365', 'OD', 'PLANNER', 'PP', 'SC', 'SENTINEL', 'SH', 'SPO', 'TEAMS')
77
$Script:M365DSCDependenciesValidated = $false
88
$Script:IsPowerShellCore = $PSVersionTable.PSEdition -eq 'Core'
9+
$Script:IsPsResourceGetAvailable = $null -ne (Get-Module -Name Microsoft.PowerShell.PSResourceGet -ListAvailable)
910
$Script:M365DSCStringReplacementMap = @{}
1011
if ($null -eq $Script:M365DSCDependencies)
1112
{
@@ -3437,6 +3438,12 @@ Specifies that the function should only return the dependencies that are not ins
34373438
.Parameter Scope
34383439
Specifies the scope of the update of the module. The default value is AllUsers(needs to run as elevated user).
34393440
3441+
.PARAMETER Proxy
3442+
Specifies the proxy server to use for the module installation.
3443+
3444+
.PARAMETER Repository
3445+
Specifies the PowerShell repository name to use for the installation of the dependencies.
3446+
34403447
.Example
34413448
Update-M365DSCDependencies
34423449
@@ -3468,7 +3475,11 @@ function Update-M365DSCDependencies
34683475

34693476
[Parameter()]
34703477
[System.String]
3471-
$Proxy
3478+
$Proxy,
3479+
3480+
[Parameter()]
3481+
[System.String]
3482+
$Repository = 'PSGallery'
34723483
)
34733484

34743485
try
@@ -3484,6 +3495,28 @@ function Update-M365DSCDependencies
34843495
$params.Add('Proxy', $Proxy)
34853496
}
34863497

3498+
# Check if PSResourceGet is installed or not
3499+
if (-not $Script:IsPsResourceGetAvailable)
3500+
{
3501+
Write-Warning -Message 'Microsoft.PowerShell.PSResourceGet is not installed, installing it now...'
3502+
try
3503+
{
3504+
Install-Module -Name Microsoft.PowerShell.PSResourceGet -Scope $Scope -AllowClobber @params -Force -ErrorAction Stop -Repository PSGallery
3505+
$Script:IsPsResourceGetAvailable = $true
3506+
}
3507+
catch
3508+
{
3509+
Write-Warning -Message "Failed to install Microsoft.PowerShell.PSResourceGet, continuing without it..."
3510+
}
3511+
}
3512+
3513+
$scopedIsPsResourceGetAvailable = $Script:IsPsResourceGetAvailable
3514+
if ($params.ContainsKey('Proxy'))
3515+
{
3516+
Write-Information -MessageData "Falling back to Install-Module because Install-PSResource does not support a proxy"
3517+
$scopedIsPsResourceGetAvailable = $false
3518+
}
3519+
34873520
foreach ($dependency in $Script:M365DSCDependencies.Values.GetEnumerator())
34883521
{
34893522
Write-Progress -Activity 'Scanning dependencies' -PercentComplete ($i / $Script:M365DSCDependencies.Count * 100)
@@ -3532,14 +3565,23 @@ function Update-M365DSCDependencies
35323565
continue
35333566
}
35343567

3535-
Write-Information -MessageData "Installing $($dependency.ModuleName) version {$($dependency.RequiredVersion)}"
35363568
Remove-Module $dependency.ModuleName -Force -ErrorAction SilentlyContinue
35373569
if ($dependency.ModuleName -like 'Microsoft.Graph*')
35383570
{
35393571
Remove-Module 'Microsoft.Graph.Authentication' -Force -ErrorAction SilentlyContinue
35403572
}
35413573
Remove-Module $dependency.ModuleName -Force -ErrorAction SilentlyContinue
3542-
Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -AllowClobber -Force -Scope "$Scope" @Params
3574+
3575+
if ($scopedIsPsResourceGetAvailable)
3576+
{
3577+
Write-Information -MessageData "Using Install-PSResource to install $($dependency.ModuleName) with version {$($dependency.RequiredVersion)}"
3578+
Install-PSResource -Name $dependency.ModuleName -Version $dependency.RequiredVersion -Scope $Scope -AcceptLicense -SkipDependencyCheck -TrustRepository -Repository $Repository
3579+
}
3580+
else
3581+
{
3582+
Write-Information -MessageData "Using Install-Module to install $($dependency.ModuleName) with version {$($dependency.RequiredVersion)}"
3583+
Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -AllowClobber -Force -Scope "$Scope" @Params -Repository $Repository
3584+
}
35433585
}
35443586
}
35453587

@@ -4930,6 +4972,18 @@ This function updates the module, dependencies and uninstalls outdated dependenc
49304972
.Parameter Scope
49314973
Specifies the scope of the update of the module. The default value is AllUsers(needs to run as elevated user).
49324974
4975+
.PARAMETER Proxy
4976+
Specifies the proxy server to use for the update.
4977+
4978+
.PARAMETER BaseRepository
4979+
Specifies the PowerShell Repository name to use for the installation of the Microsoft365DSC module.
4980+
4981+
.PARAMETER DependencyRepository
4982+
Specifies the PowerShell Repository name to use for the installation of the dependencies of the Microsoft365DSC module.
4983+
4984+
.PARAMETER NoUninstall
4985+
Indicates if outdated dependencies and modules should be uninstalled.
4986+
49334987
.Example
49344988
Update-M365DSCModule
49354989
@@ -4954,6 +5008,14 @@ function Update-M365DSCModule
49545008
[System.String]
49555009
$Proxy,
49565010

5011+
[Parameter()]
5012+
[System.String]
5013+
$BaseRepository = 'PSGallery',
5014+
5015+
[Parameter()]
5016+
[System.String]
5017+
$DependencyRepository = 'PSGallery',
5018+
49575019
[Parameter()]
49585020
[switch]
49595021
$NoUninstall
@@ -4973,7 +5035,12 @@ function Update-M365DSCModule
49735035
{
49745036
if ($_.Exception.Message -like "*Module 'Microsoft365DSC' was not installed by using Install-Module")
49755037
{
4976-
Write-Verbose -Message "The Microsoft365DSC module was not installed from the PowerShell Gallery and therefore cannot be updated."
5038+
Write-Verbose -Message "The Microsoft365DSC module might have been installed with Install-PSResource"
5039+
if ($null -ne (Get-Module -Name Microsoft.PowerShell.PSResourceGet -ListAvailable))
5040+
{
5041+
Write-Verbose -Message "Updating the Microsoft365DSC module using Update-PSResource..."
5042+
Update-PSResource -Name 'Microsoft365DSC' -Scope $Scope -TrustRepository -AcceptLicense -SkipDependencyCheck -Repository $BaseRepository
5043+
}
49775044
}
49785045
}
49795046
try
@@ -4996,7 +5063,8 @@ function Update-M365DSCModule
49965063
-Source $($MyInvocation.MyCommand.Source)
49975064
throw $_
49985065
}
4999-
Update-M365DSCDependencies -Scope $Scope -Proxy $Proxy
5066+
5067+
Update-M365DSCDependencies -Scope $Scope -Proxy $Proxy -Repository $DependencyRepository
50005068

50015069
if (-not $NoUninstall)
50025070
{

0 commit comments

Comments
 (0)