Created
September 8, 2021 18:33
-
-
Save jrotello/74cd969c877b12a22d3ee35f03210eda to your computer and use it in GitHub Desktop.
Export a PFX (certificate and key) from Azure Key Vault
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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