CheckVersion.ps1


function Invoke-CheckCyberAssessmentVersion {
    <#
    .SYNOPSIS
    Complain if a newer version of CyberAssessment is available from PSGallery.
 
    .DESCRIPTION
    Checks latest version available on PSGallery and compares it to the current running version.
    #>

    try{
        $CyberManifest = Import-PowerShellDataFile (Join-Path -Path $PSScriptRoot -ChildPath 'CyberAssessment.psd1' -Resolve  -ErrorAction 'Stop' ) -ErrorAction 'Stop'
        $CurrentVersion = [System.Version]$CyberManifest.ModuleVersion
        $LatestModule = Find-Module -Name "CLA-Assessment" -Repository PSGallery -ErrorAction SilentlyContinue
        if ($null -eq $LatestModule) {
            $LatestModule = Find-Module -Name "CyberAssessment" -Repository PSGallery -ErrorAction SilentlyContinue
        }
        $LatestVersion = if ($null -ne $LatestModule) { [System.Version]$LatestModule.Version } else { $null }

        if ($null -ne $LatestVersion -and $CurrentVersion -lt $LatestVersion) {
            Write-Warning "A newer version of CyberAssessment ($LatestVersion) is available. Please consider running: Update-CyberAssessment, this notification can be disabled by setting `$env:CYBERASSESSMENT_SKIP_VERSION_CHECK = `$true before running CyberAssessment."
        }
    }
    catch{
        Write-Warning "The CyberAssessment version check failed: $($_.Exception.Message)"
    }
}

# Do the version check if the skip envvar is not defined.
if ([string]::IsNullOrWhiteSpace($env:CYBERASSESSMENT_SKIP_VERSION_CHECK)) {
    try {
        Invoke-CheckCyberAssessmentVersion -ErrorAction 'Stop'
    }
    catch {
        Write-Warning "The CyberAssessment version check failed to execute. This notification can be disabled by setting `$env:CYBERASSESSMENT_SKIP_VERSION_CHECK = `$true.`n$($_.Exception.Message)`n$($_.ScriptStackTrace)"
    }
}