ExportCertFromRawData.psm1

<#
 .Synopsis
  Displays and Exports the certificate (in .cer format) to Home directory.
 
 .Description
  Displays and Exports the certificate (in .cer format) to Home directory.
 
 .Parameter CertRawData
  Raw Public Data.
 
 .Example
   # Displays and Exports all the certs (in .cer format) uploaded to the selected AppGW.
   Export-CertFromRawData
    
   Enter the raw data of the cert: enter the long string without qoutes
   
 
#>




function Export-CertFromRawData {
param(
$CertRawData
)

#Script to get certificate from the public data of the certificate

#Get the Raw data from user
$CertRawData = Read-Host "Enter the raw data of the cert"

#Get the data into a single line
$CertRawData = $CertRawData | ForEach-Object {$_ -replace "\r\n" -replace ".*-----BEGIN CERTIFICATE-----"  -replace "-----END CERTIFICATE-----.*"}

#Get Path to export the cert
$CertExportpath = $HOME

#cmd to convert raw data to thumbprint.
$Settingpfx= New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$Settingpfx.Import([System.Convert]::FromBase64String($CertRawData),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)

 
#Output - Name and Thumbprint of the Setting certificate
$Settingpfx | FL -Property Thumbprint, Subject, Issuer, Notafter 


#To export the certificates. If you do not want to export certificate, comment these line.
$certexpo = $Settingpfx.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "")
$CertExportpath1 = $CertExportpath+'\'+$($Settingpfx.Thumbprint)+'.cer'
[io.file]::WriteAllBytes($CertExportpath1, $certexpo)

Write-Host "Certificate $($Settingpfx.Thumbprint).cer exported to $CertExportpath1" -ForegroundColor Green
Start-Process $HOME


}

Export-ModuleMember -Function Export-CertFromRawData