public/Test-AppVentiXIsLicensed.ps1

<#
    .SYNOPSIS
        Checks if the AppVentiX license is valid.
 
    .DESCRIPTION
        The Test-AppVentiXIsLicensed function checks if the AppVentiX license is valid. It retrieves the license details and compares the expiration date with the current date. If the license is expired or about to expire within 30 days, a warning message is displayed. If the license is valid, the function returns $true; otherwise, it returns $false.
 
    .PARAMETER ConfigShare
        Specifies the path to the AppVentiX configuration share.
        You can ommit this parameter if Set-AppVentiXConfigShare has been called.
 
    .PARAMETER Force
        Forces the license validation even if it has already been validated.
 
    .EXAMPLE
        PS C:\> Test-AppVentiXIsLicensed -ConfigShare "\\server\share" -Force
        Checks if the license is valid for the AppVentiX configuration share "\\server\share" and forces the validation.
 
    .NOTES
        Function : Test-AppVentiXIsLicensed
        Author : John Billekens
        Copyright: Copyright (c) AppVentiX
        Version : 1.0
#>

function Test-AppVentiXIsLicensed {
    [CmdletBinding()]
    [OutputType([bool])]
    param (
        [Parameter()]
        [String]$ConfigShare = $Script:AppVentix.ConfigShare,

        [Switch]$Force
    )
    try {
        $licenseIsValid = $Script:AppVentix.LicenseValid
        if (-Not ($licenseIsValid -is [bool])) {
            $licenseIsValid = $false
        }
        if (($licenseIsValid -eq $false) -or $Force) {
            if (Test-Path -Path $ConfigShare) {
                Write-Verbose "Checking if the license is valid..."
                $licenseDetails = Get-AppVentiXLicence -ConfigShare $ConfigShare
                Write-Verbose "License details:`r`n$(($licenseDetails | Out-String).Trim())"
                if ($licenseDetails.LicensedUntil -gt [datetime]::Now) {
                    if (($licenseDetails.LicensedUntil - [datetime]::Now).Days -lt 30) {
                        Write-Warning "The license will expire on $($licenseDetails.LicensedUntil.ToString("M/d/yyyy")). This means that AppVentiX will stop working.`r`nPlease contact support@appventix.com to extend the license subscription."
                    }
                    Write-Verbose "AppVentiX license is valid until $($licenseDetails.LicensedUntil)"
                    $licenseIsValid = $true
                } else {
                    Write-Warning "AppVentiX license expired on $($licenseDetails.LicensedUntil)!"
                    $licenseIsValid = $false
                }
            } else {
                Write-Warning "Config share not accessable!"
                $licenseIsValid = $false
            }
        } else {
            Write-Verbose "License already validated and valid, skipping..."
            $licenseIsValid = $true
        }
    } catch {
        Write-Verbose "Error reading the license file, $($_.Exception.Message)"
        $Script:AppVentix.LicenseValid = $false
        Throw "Could not read the license file 'AppVentiX.lic'!"
    }
    $Script:AppVentix.LicenseValid = $licenseIsValid
    return $licenseIsValid
}