Skip to content

Commit

Permalink
Merge pull request #2120 from t0w0ru/t0w0ru-patch-1
Browse files Browse the repository at this point in the history
Add function Test-M365DSCModuleValidity
  • Loading branch information
NikCharlebois authored Jul 20, 2022
2 parents dbee894 + 211f2b5 commit 5e44e5b
Showing 1 changed file with 64 additions and 7 deletions.
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'
)

0 comments on commit 5e44e5b

Please sign in to comment.