Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function Test-M365DSCModuleValidity #2120

Merged
merged 3 commits into from
Jul 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
{
Expand All @@ -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" }

Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -3519,5 +3575,6 @@ Export-ModuleMember -Function @(
'Test-M365DSCParameterState',
'Uninstall-M365DSCOutdatedDependencies',
'Update-M365DSCDependencies',
'Update-M365DSCExportAuthenticationResults'
'Update-M365DSCExportAuthenticationResults',
'Test-M365DSCModuleValidity'
)