From 10a3240ce80ffc8a323d852b7bf2c98e58c807cb Mon Sep 17 00:00:00 2001 From: t0ru <52724219+t0w0ru@users.noreply.github.com> Date: Wed, 20 Jul 2022 10:19:55 +0200 Subject: [PATCH 1/3] Add function Test-M365DSCModuleValidity this changes the behavior of Test-M365DSCDependenciesForNewVersions by having it return an array of outdated modules, technically this step can be skipped/replaced by just the Update-M365DSCDependencies and Uninstall-M365DSCOutdatedDependencies cmdlets --- .../Microsoft365DSC/Modules/M365DSCUtil.psm1 | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 index 5ad2b9ec67..b09301f541 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 @@ -2341,6 +2341,8 @@ function Test-M365DSCDependenciesForNewVersions $dependencies = $manifest.Dependencies $i = 1 Import-Module PowerShellGet -Force + + $OutdatedModules = [System.Collections.ArrayList]@() foreach ($dependency in $dependencies) { Write-Progress -Activity "Scanning Dependencies" -PercentComplete ($i / $dependencies.Count * 100) @@ -2356,6 +2358,11 @@ function Test-M365DSCDependenciesForNewVersions if (-not $modules -or [Version]($moduleInGallery.Version) -gt [Version]($moduleInstalled[0].Version)) { Write-Host "New version of {$($dependency.ModuleName)} is available {$($moduleInGallery.Version)}" + $DependencyObject = [PSCustomObject]@{ + ModuleName = $($dependency.ModuleName) + ModuleVersion = $($moduleInGallery.Version) + } + $OutdatedModules.Add($DependencyObject) } } catch @@ -2365,6 +2372,7 @@ function Test-M365DSCDependenciesForNewVersions } $i++ } + return $OutdatedModules } <# @@ -3482,6 +3490,60 @@ function New-M365DSCMissingResourcesExample } } + + + +<# +.Description +This function validates there are no updates to the module or it's dependencies and no multiple versions are present on the local system. + +.Parameter Force +Specifies that all dependencies should be forcefully imported again. + +.Example +Test-M365DSCModuleValidity + +.Example +Test-M365DSCModuleValidity -Force + +.Functionality +Public +#> +function Test-M365DSCModuleValidity +{ + [CmdletBinding()] + param( + [parameter()] + [Switch] + $Force + ) + $InformationPreference = 'Continue' + + # validate only one installation of the module is present (and it's the latest version available from the psgallery) + $latestVersion = (Find-Module -Name 'Microsoft365DSC').Version + Install-Module -Name 'Microsoft365DSC' -Force -AllowClobber -RequiredVersion $latestVersion + + [array]$modules = Get-Module -Name 'Microsoft365DSC' -List | Sort-Object Version -Descending + + if ($null -ne $modules[1]) { + # multiple installations of the module are present on the system + Write-Verbose "Removing outdated installations of the Microsoft365DSC module" + for ($i = 1; $i -lt $array.Count; $i++) { # skip the first element + if (Test-Path -Path $module[$i].Path) + { + Remove-Item $module[$i].Path -Force -Recurse -ErrorAction SilentlyContinue + } + } + } + + # validate dependencies are up to date + $OutdatedDependencies = Test-M365DSCDependenciesForNewVersions + if ($OutdatedDependencies.Count -gt 0) { + Update-M365DSCDependencies -Force + } + + Uninstall-M365DSCOutdatedDependencies +} Export-ModuleMember -Function @( 'Assert-M365DSCBlueprint', 'Confirm-ImportedCmdletIsAvailable', From e85f1973fd76bd026c4d7b652fccee01ae5c569d Mon Sep 17 00:00:00 2001 From: Finn Driediger <52724219+t0w0ru@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:37:31 +0200 Subject: [PATCH 2/3] Update M365DSCUtil.psm1 --- .../Microsoft365DSC/Modules/M365DSCUtil.psm1 | 65 +++++++++---------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 index b09301f541..eea5922e41 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 @@ -2342,7 +2342,6 @@ function Test-M365DSCDependenciesForNewVersions $i = 1 Import-Module PowerShellGet -Force - $OutdatedModules = [System.Collections.ArrayList]@() foreach ($dependency in $dependencies) { Write-Progress -Activity "Scanning Dependencies" -PercentComplete ($i / $dependencies.Count * 100) @@ -2358,11 +2357,6 @@ function Test-M365DSCDependenciesForNewVersions if (-not $modules -or [Version]($moduleInGallery.Version) -gt [Version]($moduleInstalled[0].Version)) { Write-Host "New version of {$($dependency.ModuleName)} is available {$($moduleInGallery.Version)}" - $DependencyObject = [PSCustomObject]@{ - ModuleName = $($dependency.ModuleName) - ModuleVersion = $($moduleInGallery.Version) - } - $OutdatedModules.Add($DependencyObject) } } catch @@ -2372,7 +2366,6 @@ function Test-M365DSCDependenciesForNewVersions } $i++ } - return $OutdatedModules } <# @@ -2485,14 +2478,18 @@ function Uninstall-M365DSCOutdatedDependencies $InformationPreference = 'Continue' - $microsoft365DscModules = Get-Module Microsoft365DSC -ListAvailable + [array]$microsoft365DscModules = Get-Module Microsoft365DSC -ListAvailable $outdatedMicrosoft365DscModules = $microsoft365DscModules | Sort-Object Version | Select-Object -SkipLast 1 foreach ($module in $outdatedMicrosoft365DscModules) { try { - Uninstall-Module -Name "$($module.Name)" -RequiredVersion "$($module.Version)" + Write-Information -Message "Uninstalling $($module.Name) Version {$($module.Version)}" + if (Test-Path -Path $($module.Path)) + { + Remove-Item $($module.Path) -Force -Recurse + } } catch { @@ -2501,7 +2498,7 @@ function Uninstall-M365DSCOutdatedDependencies } $currentPath = Join-Path -Path $PSScriptRoot -ChildPath '..\' -Resolve - $manifest = Import-PowerShellDataFile "$currentPath/Dependencies/Manifest.psd1" + $manifest = Import-PowerShellDataFile "$currentPath\Dependencies\Manifest.psd1" $allDependenciesExceptAuth = $manifest.Dependencies | Where-Object { $_.ModuleName -ne "Microsoft.Graph.Authentication" } @@ -2516,8 +2513,11 @@ function Uninstall-M365DSCOutdatedDependencies { try { - Write-Information -Message "Uninstalling $($foundModule.Name) version {$($foundModule.Version)}" - Uninstall-Module -Name "$($foundModule.Name)" -RequiredVersion "$($foundModule.Version)" + Write-Information -Message "Uninstalling $($foundModule.Name) Version {$($foundModule.Version)}" + if (Test-Path -Path $($foundModule.Path)) + { + Remove-Item $($foundModule.Path) -Force -Recurse + } } catch { @@ -2542,7 +2542,10 @@ function Uninstall-M365DSCOutdatedDependencies try { Write-Information -Message "Uninstalling $($foundModule.Name) version {$($foundModule.Version)}" - Uninstall-Module -Name "$($foundModule.Name)" -RequiredVersion "$($foundModule.Version)" -Force + if (Test-Path -Path $($foundModule.Path)) + { + Remove-Item $($foundModule.Path) -Force -Recurse + } } catch { @@ -3513,36 +3516,25 @@ function Test-M365DSCModuleValidity { [CmdletBinding()] param( - [parameter()] - [Switch] - $Force ) $InformationPreference = 'Continue' # validate only one installation of the module is present (and it's the latest version available from the psgallery) $latestVersion = (Find-Module -Name 'Microsoft365DSC').Version - Install-Module -Name 'Microsoft365DSC' -Force -AllowClobber -RequiredVersion $latestVersion - - [array]$modules = Get-Module -Name 'Microsoft365DSC' -List | Sort-Object Version -Descending + $localVersion = (Get-Module -Name 'Microsoft365DSC').Version - if ($null -ne $modules[1]) { - # multiple installations of the module are present on the system - Write-Verbose "Removing outdated installations of the Microsoft365DSC module" - for ($i = 1; $i -lt $array.Count; $i++) { # skip the first element - if (Test-Path -Path $module[$i].Path) - { - Remove-Item $module[$i].Path -Force -Recurse -ErrorAction SilentlyContinue - } - } - } - - # validate dependencies are up to date - $OutdatedDependencies = Test-M365DSCDependenciesForNewVersions - if ($OutdatedDependencies.Count -gt 0) { + if ($latestVersion -gt $localVersion) + { + Write-Host "There is a newer version of the 'Microsoft365DSC' module available on the gallery." + if(!( $UpdateConsent = Read-Host -Prompt "Do you wish to update the M365DSC module and it's dependencies? (Y/N) [Default: 'Y']")) { $UpdateConsent = 'Y' } + if(!( $UpdateConsent -eq 'Y' -or $UpdateConsent -eq 'y' )) { return } + Write-Host "Updating the M365DSC module..." -ForegroundColor Yellow + Update-Module -Name 'Microsoft365DSC' -Force + Write-Host "Updating dependencies..." -ForegroundColor Yellow Update-M365DSCDependencies -Force + Write-Host "uninstalling outdated installations..." -ForegroundColor Yellow + Uninstall-M365DSCOutdatedDependencies } - - Uninstall-M365DSCOutdatedDependencies } Export-ModuleMember -Function @( 'Assert-M365DSCBlueprint', @@ -3581,5 +3573,6 @@ Export-ModuleMember -Function @( 'Test-M365DSCParameterState', 'Uninstall-M365DSCOutdatedDependencies', 'Update-M365DSCDependencies', - 'Update-M365DSCExportAuthenticationResults' + 'Update-M365DSCExportAuthenticationResults', + 'Test-M365DSCModuleValidity' ) From 211f2b5e8f8fc115151e7bed636fc00524d99374 Mon Sep 17 00:00:00 2001 From: Finn Driediger <52724219+t0w0ru@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:52:43 +0200 Subject: [PATCH 3/3] Update M365DSCUtil.psm1 --- .../Microsoft365DSC/Modules/M365DSCUtil.psm1 | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 index eea5922e41..2dca1b3b82 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 @@ -3526,14 +3526,16 @@ function Test-M365DSCModuleValidity if ($latestVersion -gt $localVersion) { Write-Host "There is a newer version of the 'Microsoft365DSC' module available on the gallery." - if(!( $UpdateConsent = Read-Host -Prompt "Do you wish to update the M365DSC module and it's dependencies? (Y/N) [Default: 'Y']")) { $UpdateConsent = 'Y' } - if(!( $UpdateConsent -eq 'Y' -or $UpdateConsent -eq 'y' )) { return } - Write-Host "Updating the M365DSC module..." -ForegroundColor Yellow - Update-Module -Name 'Microsoft365DSC' -Force - Write-Host "Updating dependencies..." -ForegroundColor Yellow - Update-M365DSCDependencies -Force - Write-Host "uninstalling outdated installations..." -ForegroundColor Yellow - Uninstall-M365DSCOutdatedDependencies + Write-Host "To update the module and it's dependencies, run the following commands:" + Write-Host "Update-Module -Name 'Microsoft365DSC' -Force`nUpdate-M365DSCDependencies -Force`nUninstall-M365DSCOutdatedDependencies" -ForegroundColor Blue + # if(!( $UpdateConsent = Read-Host -Prompt "Do you wish to update the M365DSC module and it's dependencies? (Y/N) [Default: 'Y']")) { $UpdateConsent = 'Y' } + # if(!( $UpdateConsent -eq 'Y' -or $UpdateConsent -eq 'y' )) { return } + # Write-Host "Updating the M365DSC module..." -ForegroundColor Yellow + # Update-Module -Name 'Microsoft365DSC' -Force + # Write-Host "Updating dependencies..." -ForegroundColor Yellow + #Update-M365DSCDependencies -Force + # Write-Host "uninstalling outdated installations..." -ForegroundColor Yellow + # Uninstall-M365DSCOutdatedDependencies } } Export-ModuleMember -Function @(