Sample code for 30+ languages & platforms
PowerShell Requires Chilkat v11.6.0+

Azure Key Vault: Back Up a Key, Secret, or Certificate

See more Azure Key Vault Examples

Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.

Uses HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.

Choose object_kind and object_name. Requires the corresponding backup permission. Saves the opaque base64url backup plus kind/name metadata to a new private qa_output/keyvault-backup.json. Includes versions supported by the backup API; it is not a portable plaintext/private-key export. Restore requires compatible service subscription/geographic boundaries and a free destination name. Protect backup blobs and observe service limits on version counts.

Microsoft reference

Chilkat PowerShell Downloads

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

$stringCompare = New-Object Chilkat.StringBuilder
#  Assumes Chilkat is unlocked. See Global Unlock Sample.
#  Keep this configuration private, outside public web content and source control.
$config = New-Object Chilkat.JsonObject
$config.Strict = $true
$success = $config.LoadFile("qa_data/azure/keyvault.json")
if ($success -ne $true) {
    $("Could not load private Key Vault configuration.")
    exit
}

$stringCompare.SetString($config.StringOf("vault_name"))
$stringMatch1 = $stringCompare.ContentsEqual("",$true)
if (($config.JsonTypeOf("vault_name") -ne 1) -or ($stringMatch1 -eq $true)) {
    $("Missing or invalid vault_name.")
    exit
}

$validate1 = New-Object Chilkat.StringBuilder
$alphabet1 = New-Object Chilkat.StringBuilder
$success = $validate1.Append($config.StringOf("vault_name"))
if ($success -ne $true) {
    $("Could not validate vault_name.")
    exit
}

$success = $alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
if ($success -ne $true) {
    $("Could not initialize validation.")
    exit
}

if (($validate1.Length -lt 3) -or ($validate1.Length -gt 24)) {
    $("Invalid vault_name length.")
    exit
}

$charIndex1 = 0
while (($charIndex1 -lt $validate1.Length)) {
    if ($alphabet1.Contains($validate1.GetRange($charIndex1,1,$false),$true) -ne $true) {
        $("Invalid character in vault_name.")
        exit
    }

    $charIndex1 = $charIndex1 + 1
}

$vaultName = New-Object Chilkat.StringBuilder
$vaultName.Append($config.StringOf("vault_name"))
$letters = New-Object Chilkat.StringBuilder
$letters.Append("abcdefghijklmnopqrstuvwxyz")
if (($letters.Contains($vaultName.GetRange(0,1,$false),$true) -ne $true) -or ($vaultName.EndsWith("-",$true) -eq $true) -or ($vaultName.Contains("--",$true) -eq $true)) {
    $("Invalid vault_name.")
    exit
}

$origin = New-Object Chilkat.StringBuilder
$success = $origin.Append("https://")
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $origin.Append($config.StringOf("vault_name"))
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $origin.Append(".vault.azure.net")
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.FollowRedirects = $false
$http.ConnectTimeout = 20
$http.ReadTimeout = 60
$http.VerboseLogging = $false
$request = New-Object Chilkat.HttpRequest
$request.HttpVerb = "GET"
$response = New-Object Chilkat.HttpResponse
$result = New-Object Chilkat.JsonObject
$result.Strict = $true
#  First run Get a Key Vault access token.
$token = New-Object Chilkat.JsonObject
$token.Strict = $true
$success = $token.LoadFile("qa_data/tokens/azureKeyVaultToken.json")
if ($success -ne $true) {
    $("Could not read the private token file.")
    exit
}

$stringCompare.SetString($token.StringOf("access_token"))
$stringMatch2 = $stringCompare.ContentsEqual("",$true)
if (($token.JsonTypeOf("access_token") -ne 1) -or ($stringMatch2 -eq $true)) {
    $("Missing or invalid access_token.")
    exit
}

$stringCompare.SetString($token.StringOf("expires_at"))
$stringMatch3 = $stringCompare.ContentsEqual("",$true)
if (($token.JsonTypeOf("expires_at") -ne 1) -or ($stringMatch3 -eq $true)) {
    $("Missing or invalid expires_at.")
    exit
}

$expires = New-Object Chilkat.CkDateTime
$now = New-Object Chilkat.CkDateTime
$success = $expires.SetFromTimestamp($token.StringOf("expires_at"))
if ($success -ne $true) {
    $("Invalid token expiration.")
    exit
}

$http.AuthToken = $token.StringOf("access_token")
$stringCompare.SetString($config.StringOf("object_kind"))
$stringMatch4 = $stringCompare.ContentsEqual("",$true)
if (($config.JsonTypeOf("object_kind") -ne 1) -or ($stringMatch4 -eq $true)) {
    $("Missing or invalid object_kind.")
    exit
}

$stringCompare.SetString($config.StringOf("object_kind"))
$stringMatch5 = $stringCompare.ContentsEqual("keys",$true)
$stringCompare.SetString($config.StringOf("object_kind"))
$stringMatch6 = $stringCompare.ContentsEqual("secrets",$true)
$stringCompare.SetString($config.StringOf("object_kind"))
$stringMatch7 = $stringCompare.ContentsEqual("certificates",$true)
if (($stringMatch5 -ne $true) -and ($stringMatch6 -ne $true) -and ($stringMatch7 -ne $true)) {
    $("object_kind must be keys, secrets, or certificates.")
    exit
}

$stringCompare.SetString($config.StringOf("object_name"))
$stringMatch8 = $stringCompare.ContentsEqual("",$true)
if (($config.JsonTypeOf("object_name") -ne 1) -or ($stringMatch8 -eq $true)) {
    $("Missing or invalid object_name.")
    exit
}

$validate51 = New-Object Chilkat.StringBuilder
$alphabet51 = New-Object Chilkat.StringBuilder
$success = $validate51.Append($config.StringOf("object_name"))
if ($success -ne $true) {
    $("Could not validate object_name.")
    exit
}

$success = $alphabet51.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
if ($success -ne $true) {
    $("Could not initialize validation.")
    exit
}

if (($validate51.Length -lt 1) -or ($validate51.Length -gt 127)) {
    $("Invalid object_name length.")
    exit
}

$charIndex51 = 0
while (($charIndex51 -lt $validate51.Length)) {
    if ($alphabet51.Contains($validate51.GetRange($charIndex51,1,$false),$true) -ne $true) {
        $("Invalid character in object_name.")
        exit
    }

    $charIndex51 = $charIndex51 + 1
}

$resourceBase = New-Object Chilkat.StringBuilder
$success = $resourceBase.Append($origin.GetAsString())
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $resourceBase.Append("/")
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $resourceBase.Append($config.StringOf("object_kind"))
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $resourceBase.Append("/")
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $resourceBase.Append($config.StringOf("object_name"))
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$deletedBase = New-Object Chilkat.StringBuilder
$success = $deletedBase.Append($origin.GetAsString())
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $deletedBase.Append("/deleted")
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $deletedBase.Append($config.StringOf("object_kind"))
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $deletedBase.Append("/")
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $deletedBase.Append($config.StringOf("object_name"))
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$files = New-Object Chilkat.FileAccess
if ($files.FileExists("qa_output/keyvault-backup.json") -eq $true) {
    $("Output exists; choose another private output path.")
    exit
}

$backupUrl = New-Object Chilkat.StringBuilder
$success = $backupUrl.Append($resourceBase.GetAsString())
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $backupUrl.Append("/backup?api-version=2026-04-01")
if ($success -ne $true) {
    $("Could not construct URL or identifier.")
    exit
}

$success = $now.SetFromCurrentSystemTime()
if ($success -ne $true) {
    $("Could not read current time.")
    exit
}

if ($expires.DiffSeconds($now) -le 60) {
    $("Token is expired or near expiration. Obtain a new token before continuing.")
    exit
}

$request.HttpVerb = "POST"
$success = $http.HttpReq($backupUrl.GetAsString(),$request,$response)
if ($success -ne $true) {
    $("Request did not complete; inspect diagnostics privately.")
    exit
}

if ($response.StatusCode -ne 200) {
    $("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
    exit
}

$success = $result.Load($response.BodyStr)
if ($success -ne $true) {
    $("Invalid JSON response.")
    exit
}

$stringCompare.SetString($result.StringOf("value"))
$stringMatch9 = $stringCompare.ContentsEqual("",$true)
if (($result.JsonTypeOf("value") -ne 1) -or ($stringMatch9 -eq $true)) {
    $("Missing or invalid value.")
    exit
}

$validate53 = New-Object Chilkat.StringBuilder
$alphabet53 = New-Object Chilkat.StringBuilder
$success = $validate53.Append($result.StringOf("value"))
if ($success -ne $true) {
    $("Could not validate backup blob.")
    exit
}

$success = $alphabet53.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
if ($success -ne $true) {
    $("Could not initialize validation.")
    exit
}

if (($validate53.Length -lt 1) -or ($validate53.Length -gt 10485760)) {
    $("Invalid backup blob length.")
    exit
}

$charIndex53 = 0
while (($charIndex53 -lt $validate53.Length)) {
    if ($alphabet53.Contains($validate53.GetRange($charIndex53,1,$false),$true) -ne $true) {
        $("Invalid character in backup blob.")
        exit
    }

    $charIndex53 = $charIndex53 + 1
}

$backup = New-Object Chilkat.JsonObject
$success = $backup.UpdateString("kind",$config.StringOf("object_kind"))
if ($success -ne $true) {
    $("Could not set kind.")
    exit
}

$success = $backup.UpdateString("name",$config.StringOf("object_name"))
if ($success -ne $true) {
    $("Could not set name.")
    exit
}

$success = $backup.UpdateString("value",$result.StringOf("value"))
if ($success -ne $true) {
    $("Could not set value.")
    exit
}

$success = $backup.WriteFile("qa_output/keyvault-backup.json")
if ($success -ne $true) {
    $("Could not save protected backup blob.")
    exit
}

$("Example completed.")