Public/Get-P1ClientPublicCertificate.ps1

function Get-P1ClientPublicCertificate {
    <#
    .Synopsis
    Get the public client certificate.

    .Description
    If PlannerOne is configured with certificate authentication with ERP, a certificate should have been set with Set-P1AdapterParameters.
    This command returns the public certificate associated to the PlannerOne client PKCS#12 (pfx) certificate.

    .Parameter Tenant
    The tenant name.

    .Parameter OutFile
    The cer file path to store public key in.

    .Example
    # Get public certificate for tenant Prod
    Get-P1ClientPublicCertificate -Tenant Prod
    #>

    [cmdletbinding()]
    param( 
        [Parameter(Mandatory=$true)]
        [string] $Tenant,
        [string] $OutFile
    )
    Process
    {
        Write-Section "Exporting public certificate for client"
        if (!(Test-Tenant $Tenant)) {
            Write-Warning "Tenant $Tenant does not exist."
            Write-Warning "Operation canceled."
            return
        }

        $parameters = Get-P1adapterParameters $Tenant
        if ($parameters -eq $null) {
            Write-KO "No parameters to read to get certificate informations for tenant $Tenant"
            return
        }

        [string] $certificatePath = $parameters.ClientCertificatePath
        if ($certificatePath -eq "") {
            Write-KO "No certificate defined in configuration of tenant $Tenant"
            return
        }

        Write-Output "Please enter your pfx certificate's password if needed"
        $cert = Get-PfxCertificate $certificatePath
        if ($cert -eq $null) {
            Write-KO "Error during PFX Certificate obtention"
            return
        }

        New-Item $OutFile -force | Out-Null
        Add-Content $OutFile "-----BEGIN CERTIFICATE-----"  
        $base64PublicCer = [System.Convert]::ToBase64String($cert.export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert), [System.Base64FormattingOptions]::InsertLineBreaks)
        Add-Content $OutFile $base64PublicCer
        Add-Content $OutFile "-----END CERTIFICATE-----"
        Get-Content $OutFile
        Write-OK "Public certificate exported to $OutFile"
    }
}