PowerShell
PowerShell
Add a Private Key and Certificate Chain to a PFX
See more PFX/P12 Examples
Demonstrates Pfx.AddPrivateKey, which adds a private key together with its certificate chain to the PFX object.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The chain must contain at least one certificate; the certificate at index 0 is treated as the leaf. The chain is built here with Cert.BuildCertChain.
Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
$pfx = New-Object Chilkat.Pfx
# The file path is relative to the application's current working directory. An absolute path
# may also be used. Supply the path appropriate to your own environment.
# Load the private key.
$privKey = New-Object Chilkat.PrivateKey
$success = $privKey.LoadPemFile("qa_data/private_key.pem")
if ($success -eq $false) {
$($privKey.LastErrorText)
exit
}
# Load the certificate and build its chain of authority. The chain must contain at least one
# certificate; the certificate at index 0 is treated as the leaf.
$cert = New-Object Chilkat.Cert
$success = $cert.LoadFromFile("qa_data/cert.pem")
if ($success -eq $false) {
$($cert.LastErrorText)
exit
}
$chain = New-Object Chilkat.CertChain
$success = $cert.BuildCertChain($chain)
if ($success -eq $false) {
$($cert.LastErrorText)
exit
}
# Add the private key and its certificate chain to the PFX.
$success = $pfx.AddPrivateKey($privKey,$chain)
if ($success -eq $false) {
$($pfx.LastErrorText)
exit
}
$("Private key and certificate chain added to the PFX.")