diff --git a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 index 5ad2b9ec67..2dca1b3b82 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 @@ -2341,6 +2341,7 @@ function Test-M365DSCDependenciesForNewVersions $dependencies = $manifest.Dependencies $i = 1 Import-Module PowerShellGet -Force + foreach ($dependency in $dependencies) { Write-Progress -Activity "Scanning Dependencies" -PercentComplete ($i / $dependencies.Count * 100) @@ -2477,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 { @@ -2493,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" } @@ -2508,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 { @@ -2534,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 { @@ -3482,6 +3493,51 @@ 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( + ) + $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 + $localVersion = (Get-Module -Name 'Microsoft365DSC').Version + + if ($latestVersion -gt $localVersion) + { + Write-Host "There is a newer version of the 'Microsoft365DSC' module available on the gallery." + 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 @( 'Assert-M365DSCBlueprint', 'Confirm-ImportedCmdletIsAvailable', @@ -3519,5 +3575,6 @@ Export-ModuleMember -Function @( 'Test-M365DSCParameterState', 'Uninstall-M365DSCOutdatedDependencies', 'Update-M365DSCDependencies', - 'Update-M365DSCExportAuthenticationResults' + 'Update-M365DSCExportAuthenticationResults', + 'Test-M365DSCModuleValidity' )