Export-AdfsCertificate.ps1

function Export-AdfsCertificate {
<#PSScriptInfo
.VERSION 1.0.0
 
.GUID 4f1352db-801e-4790-93be-2ecfd238c4f0
 
.AUTHOR Rich Kusak
 
.DESCRIPTION Exports an ADFS certificate to a file.
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS adfs certificates
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
#>


#Requires -Version 3.0

<#
.SYNOPSIS
    Exports an ADFS certificate to a file.
 
.DESCRIPTION
    The Export-AdfsCertificate function takes input from the Get-AdfsCertificate cmdlet and exports to a file.
    This is useful when needing to export ADFS generated certificates from a gMSA current user store.
 
.PARAMETER InputObject
    A certificate object from the Get-AdfsCertificate cmdlet.
 
.PARAMETER ContentType
    The X509 certificate content type.
 
.PARAMETER OutputFile
    The full path to a certificate file.
 
.NOTES
    Name : Export-AdfsCertificate
    Author : Rich Kusak
    Created : 2017-05-31
    LastEdit : 2017-05-31 21:53
 
.EXAMPLE
    $certTokenSigning = Get-AdfsCertificate -CertificateType Token-Signing
    Export-AdfsCertificate -InputObject $certTokenSigning -OutputFile C:\ADFS_TokenSigning.cer
 
.LINK
    Get-AdfsCertificate
    https://blogs.technet.microsoft.com/adhall/2014/02/19/how-to-export-the-ad-fs-token-signing-certificate-with-powershell/
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory,ValueFromPipeline)]
        [PSObject]$InputObject,

        [Parameter()]
        [System.Security.Cryptography.X509Certificates.X509ContentType]$ContentType = 'Cert',

        [Parameter(Mandatory)]
        [string]$OutputFile
    )

    process {

        try {
            $certBytes = $InputObject.Certificate.Export($ContentType)
            [System.IO.File]::WriteAllBytes($OutputFile, $certBytes)

        } catch {
            throw $_
        }

    } #process

} #function Export-AdfsCertificate