Skip to content

Instantly share code, notes, and snippets.

@jrotello
Created September 8, 2021 18:33
Show Gist options
  • Save jrotello/74cd969c877b12a22d3ee35f03210eda to your computer and use it in GitHub Desktop.
Save jrotello/74cd969c877b12a22d3ee35f03210eda to your computer and use it in GitHub Desktop.
Export a PFX (certificate and key) from Azure Key Vault
function Export-PfxCertificateFromKeyVault {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$KeyVaultName,
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[securestring]$PfxPassword,
[Parameter(Mandatory)]
[FileInfo]$Filename
)
$kvCert = Get-AzKeyVaultCertificate -VaultName $KeyVaultName -Name $Name
$base64Cert = Get-AzKeyVaultSecret -VaultName $kvCert.VaultName -Name $kvCert.Name -AsPlainText
$x509Bytes = [Convert]::FromBase64String($base64Cert)
$flags = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$x509 = New-Object `
-TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 `
-ArgumentList $x509Bytes, "", $flags
$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx
$pfxBytes = $x509.Export($type, $pw)
[System.IO.File]::WriteAllBytes($Filename, $pfxBytes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment