Public/Certificates/Test-JIMCertificate.ps1

# Copyright (c) Tetron Limited. All rights reserved.
# Licensed under the Tetron Commercial License. See LICENSE file in the project root.

function Test-JIMCertificate {
    <#
    .SYNOPSIS
        Validates a certificate in JIM's trusted certificate store.
 
    .DESCRIPTION
        Performs validation checks on a certificate including expiry date,
        chain validation, and other certificate properties.
 
    .PARAMETER Id
        The unique identifier (GUID) of the certificate to validate.
 
    .OUTPUTS
        The result of the checks. IsValid is true when the certificate passed every one; Warnings lists
        problems that do not stop it being used, such as expiring soon, and Errors those that do, such as
        having expired or an untrusted chain.
 
    .EXAMPLE
        Test-JIMCertificate -Id $certId
 
        Validates the certificate and returns the result.
 
    .EXAMPLE
        Get-JIMCertificate | Test-JIMCertificate
 
        Validates all certificates.
 
    .EXAMPLE
        Get-JIMCertificate -EnabledOnly | Test-JIMCertificate | Where-Object { -not $_.isValid }
 
        Finds all enabled certificates that fail validation.
 
    .LINK
        Get-JIMCertificate
        Add-JIMCertificate
    #>

    [CmdletBinding()]
    [OutputType('JIM.CertificateValidationResult')]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [Guid]$Id
    )

    process {
        # Check connection first
        if (-not $script:JIMConnection) {
            Write-Error "You are not connected to JIM. Run Connect-JIM -Url <your JIM URL> to authenticate, then try again."
            return
        }

        Write-Verbose "Validating certificate: $Id"

        try {
            $result = Invoke-JIMApi -Endpoint "/api/v1/certificates/$Id/validate"
            $result
        }
        catch {
            Write-Error "Failed to validate certificate: $_"
        }
    }
}