OftenOn.psm1
Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" $abort = $false <# Any modules we specify to be used with #> $configurationData = Import-PowerShellDataFile -Path "$PSScriptRoot\Configuration\OftenOn.psd1" foreach ($dscResource in $configurationData.NonNodeData.Lability.DSCResource) { [array] $modules = Get-Module $dscResource.Name -ListAvailable | Sort-Object Version -Descending if (!($dscResource.ContainsKey("RequiredVersion"))) { $abort = $true Write-Warning ".\Configuration\OftenOn.psd1 requires $($dscResource.Name) but does not have a RequiredVersion" } elseif ($dscResource.RequiredVersion -ne $modules[0].Version) { $abort = $true Write-Warning ".\Configuration\OftenOn.psd1 requires $($dscResource.Name) $($dscResource.RequiredVersion) but $($modules[0].Version) is the newest" } } Get-ChildItem $PSScriptRoot -Recurse -File | ForEach-Object { $fileName = $PSItem $content = Get-Content $fileName.FullName foreach ($line in $content) { if ($line -match "\s+Import-DscResource\s+-ModuleName\s+(.*)\s+-ModuleVersion\s+(.*)" -or $line -match "\s+Import-DscResource\s+-ModuleName\s+(.*)") { [array] $modules = Get-Module -ListAvailable $Matches[1] | Sort-Object Version -Descending if ($Matches.Count -eq 3) { # ModuleName and ModuleVersion match if ([version] $Matches[2] -ne $modules[0].Version) { # Finding a newer version on disk is not catastrophic but means we should update our references Write-Warning "$fileName requires $($Matches[1]) $($Matches[2]) but $($modules[0].Version) is the newest" } if ($modules.Version -notcontains [version] $Matches[2]) { # Not having the referenced version on disk is catastrophic $abort = $true Write-Warning "$fileName requires $($Matches[1]) $($Matches[2]) but it does not exist" } } elseif ($Matches.Count -eq 2) { # ModuleName match only if (!$modules) { Write-Warning "$fileName requires $($Matches[1]) but it does not have a ModuleVersion and none were found on disk" } else { Write-Warning "$fileName requires $($Matches[1]) but it does not have a ModuleVersion and $($modules[0].Version) is the newest" # Multiple versions on disk and nothing specified is catastrophic if ($modules.Count -ne 1) { $abort = $true Write-Warning "$fileName requires $($Matches[1]) but multiple versions exist and $($modules[0].Version) is the newest" } } } else { $abort = $true Write-Warning "Unknown number of matches when searching for broken Import-DscResource references" } } } } if ($abort) { Write-Error "DSC Modules are out of sync and this should be resolved before continuing" } Get-ChildItem $PSScriptRoot *.ps1 -Exclude *.Tests.ps1 -Recurse | ForEach-Object { Write-Verbose "Loading $($PSItem.FullName)" . $PSItem.FullName } |