Azure Key Vault: List Deleted Objects and Recover One
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. Paginates deleted objects, then inspects that object. Set allow_recover=true to recover it; otherwise the example only reads. Requires the corresponding list/get permissions and recover when enabled. A not-yet-visible deleted object can return 404; retry the read later without repeating delete. Purged objects cannot be recovered, and restored availability can be delayed.
Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
set stringCompare = CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
set config = CreateObject("Chilkat.JsonObject")
config.Strict = 1
success = config.LoadFile("qa_data/azure/keyvault.json")
If (success <> 1) Then
outFile.WriteLine("Could not load private Key Vault configuration.")
WScript.Quit
End If
success = stringCompare.SetString(config.StringOf("vault_name"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = 1)) Then
outFile.WriteLine("Missing or invalid vault_name.")
WScript.Quit
End If
set validate1 = CreateObject("Chilkat.StringBuilder")
set alphabet1 = CreateObject("Chilkat.StringBuilder")
success = validate1.Append(config.StringOf("vault_name"))
If (success <> 1) Then
outFile.WriteLine("Could not validate vault_name.")
WScript.Quit
End If
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If (success <> 1) Then
outFile.WriteLine("Could not initialize validation.")
WScript.Quit
End If
If ((validate1.Length < 3) Or (validate1.Length > 24)) Then
outFile.WriteLine("Invalid vault_name length.")
WScript.Quit
End If
charIndex1 = 0
Do While (charIndex1 < validate1.Length)
If (alphabet1.Contains(validate1.GetRange(charIndex1,1,0),1) <> 1) Then
outFile.WriteLine("Invalid character in vault_name.")
WScript.Quit
End If
charIndex1 = charIndex1 + 1
Loop
set vaultName = CreateObject("Chilkat.StringBuilder")
success = vaultName.Append(config.StringOf("vault_name"))
set letters = CreateObject("Chilkat.StringBuilder")
success = letters.Append("abcdefghijklmnopqrstuvwxyz")
If ((letters.Contains(vaultName.GetRange(0,1,0),1) <> 1) Or (vaultName.EndsWith("-",1) = 1) Or (vaultName.Contains("--",1) = 1)) Then
outFile.WriteLine("Invalid vault_name.")
WScript.Quit
End If
set origin = CreateObject("Chilkat.StringBuilder")
success = origin.Append("https://")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = origin.Append(config.StringOf("vault_name"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = origin.Append(".vault.azure.net")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = 0
set request = CreateObject("Chilkat.HttpRequest")
request.HttpVerb = "GET"
set response = CreateObject("Chilkat.HttpResponse")
set result = CreateObject("Chilkat.JsonObject")
result.Strict = 1
' First run Get a Key Vault access token.
set token = CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.LoadFile("qa_data/tokens/azureKeyVaultToken.json")
If (success <> 1) Then
outFile.WriteLine("Could not read the private token file.")
WScript.Quit
End If
success = stringCompare.SetString(token.StringOf("access_token"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((token.JsonTypeOf("access_token") <> 1) Or (stringMatch2 = 1)) Then
outFile.WriteLine("Missing or invalid access_token.")
WScript.Quit
End If
success = stringCompare.SetString(token.StringOf("expires_at"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((token.JsonTypeOf("expires_at") <> 1) Or (stringMatch3 = 1)) Then
outFile.WriteLine("Missing or invalid expires_at.")
WScript.Quit
End If
set expires = CreateObject("Chilkat.CkDateTime")
set now = CreateObject("Chilkat.CkDateTime")
success = expires.SetFromTimestamp(token.StringOf("expires_at"))
If (success <> 1) Then
outFile.WriteLine("Invalid token expiration.")
WScript.Quit
End If
http.AuthToken = token.StringOf("access_token")
success = stringCompare.SetString(config.StringOf("object_kind"))
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("object_kind") <> 1) Or (stringMatch4 = 1)) Then
outFile.WriteLine("Missing or invalid object_kind.")
WScript.Quit
End If
success = stringCompare.SetString(config.StringOf("object_kind"))
stringMatch5 = stringCompare.ContentsEqual("keys",1)
success = stringCompare.SetString(config.StringOf("object_kind"))
stringMatch6 = stringCompare.ContentsEqual("secrets",1)
success = stringCompare.SetString(config.StringOf("object_kind"))
stringMatch7 = stringCompare.ContentsEqual("certificates",1)
If ((stringMatch5 <> 1) And (stringMatch6 <> 1) And (stringMatch7 <> 1)) Then
outFile.WriteLine("object_kind must be keys, secrets, or certificates.")
WScript.Quit
End If
set collection = CreateObject("Chilkat.StringBuilder")
success = collection.Append(origin.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = collection.Append("/deleted")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = collection.Append(config.StringOf("object_kind"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set pagePrefix = CreateObject("Chilkat.StringBuilder")
success = pagePrefix.Append(collection.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = pagePrefix.Append("?")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set firstPage = CreateObject("Chilkat.StringBuilder")
success = firstPage.Append(pagePrefix.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = firstPage.Append("api-version=2026-04-01&maxresults=25")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set itemsSeen = CreateObject("Chilkat.Hashtable")
set itemsItems = CreateObject("Chilkat.JsonArray")
set itemsView = CreateObject("Chilkat.JsonArray")
set itemsEntry = CreateObject("Chilkat.JsonObject")
itemsCurrentUrl = firstPage.GetAsString()
itemsPages = 0
itemsMore = 1
Do While (itemsMore = 1)
If (itemsPages = 10000) Then
outFile.WriteLine("Page bound reached; enumeration is incomplete.")
WScript.Quit
End If
If (itemsSeen.Contains(itemsCurrentUrl) = 1) Then
outFile.WriteLine("Repeated nextLink; enumeration is incomplete.")
WScript.Quit
End If
success = itemsSeen.AddStr(itemsCurrentUrl,"seen")
If (success <> 1) Then
outFile.WriteLine("Could not track pagination.")
WScript.Quit
End If
itemsPages = itemsPages + 1
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Token is expired or near expiration. Obtain a new token before continuing.")
WScript.Quit
End If
success = http.HttpReq(itemsCurrentUrl,request,response)
If (success <> 1) Then
outFile.WriteLine("Request did not complete; inspect diagnostics privately.")
WScript.Quit
End If
If (response.StatusCode <> 200) Then
outFile.WriteLine("HTTP status: " & response.StatusCode)
outFile.WriteLine("Request ID: " & response.GetHeaderField("x-ms-request-id"))
' Do not print full response bodies, tokens, or diagnostic logs.
outFile.WriteLine("Request failed. Inspect private diagnostics; reconcile writes before retrying.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Expected a well-formed JSON response.")
WScript.Quit
End If
If (result.JsonTypeOf("value") <> 4) Then
outFile.WriteLine("Expected a value array.")
WScript.Quit
End If
success = result.ArrayOf2("value",itemsView)
If (success <> 1) Then
outFile.WriteLine("Could not read certificate array.")
WScript.Quit
End If
success = itemsItems.Load(itemsView.Emit())
If (success <> 1) Then
outFile.WriteLine("Could not detach the page before nested requests.")
WScript.Quit
End If
itemsMore = 0
If (result.HasMember("nextLink") = 1) Then
If (result.JsonTypeOf("nextLink") <> 6) Then
success = stringCompare.SetString(result.StringOf("nextLink"))
stringMatch8 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("nextLink") <> 1) Or (stringMatch8 = 1)) Then
outFile.WriteLine("Missing or invalid nextLink.")
WScript.Quit
End If
itemsCurrentUrl = result.StringOf("nextLink")
set itemsNext = CreateObject("Chilkat.StringBuilder")
success = itemsNext.Append(itemsCurrentUrl)
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
If ((itemsNext.StartsWith(pagePrefix.GetAsString(),1) <> 1) Or (itemsNext.Contains("#",1) = 1) Or (itemsNext.Contains("\",1) = 1) Or (itemsNext.Contains(vbCr,1) = 1) Or (itemsNext.Contains(vbLf,1) = 1)) Then
outFile.WriteLine("Unsafe pagination URL; stopped before sending a token.")
WScript.Quit
End If
itemsMore = 1
End If
End If
itemsIndex = 0
Do While (itemsIndex < itemsItems.Size)
success = itemsItems.ObjectAt2(itemsIndex,itemsEntry)
If (success <> 1) Then
outFile.WriteLine("Invalid item.")
WScript.Quit
End If
success = stringCompare.SetString(itemsEntry.StringOf("recoveryId"))
stringMatch9 = stringCompare.ContentsEqual("",1)
If ((itemsEntry.JsonTypeOf("recoveryId") <> 1) Or (stringMatch9 = 1)) Then
outFile.WriteLine("Missing or invalid recoveryId.")
WScript.Quit
End If
outFile.WriteLine("Resource ID: " & itemsEntry.StringOf("recoveryId"))
itemsIndex = itemsIndex + 1
Loop
Loop
success = stringCompare.SetString(config.StringOf("object_name"))
stringMatch10 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("object_name") <> 1) Or (stringMatch10 = 1)) Then
outFile.WriteLine("Missing or invalid object_name.")
WScript.Quit
End If
set validate52 = CreateObject("Chilkat.StringBuilder")
set alphabet52 = CreateObject("Chilkat.StringBuilder")
success = validate52.Append(config.StringOf("object_name"))
If (success <> 1) Then
outFile.WriteLine("Could not validate object_name.")
WScript.Quit
End If
success = alphabet52.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
outFile.WriteLine("Could not initialize validation.")
WScript.Quit
End If
If ((validate52.Length < 1) Or (validate52.Length > 127)) Then
outFile.WriteLine("Invalid object_name length.")
WScript.Quit
End If
charIndex52 = 0
Do While (charIndex52 < validate52.Length)
If (alphabet52.Contains(validate52.GetRange(charIndex52,1,0),1) <> 1) Then
outFile.WriteLine("Invalid character in object_name.")
WScript.Quit
End If
charIndex52 = charIndex52 + 1
Loop
set deletedBase = CreateObject("Chilkat.StringBuilder")
success = deletedBase.Append(origin.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = deletedBase.Append("/deleted")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = deletedBase.Append(config.StringOf("object_kind"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = deletedBase.Append("/")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = deletedBase.Append(config.StringOf("object_name"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
set getDeletedUrl = CreateObject("Chilkat.StringBuilder")
success = getDeletedUrl.Append(deletedBase.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = getDeletedUrl.Append("?api-version=2026-04-01")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Token is expired or near expiration. Obtain a new token before continuing.")
WScript.Quit
End If
request.HttpVerb = "GET"
success = http.HttpReq(getDeletedUrl.GetAsString(),request,response)
If (success <> 1) Then
outFile.WriteLine("Request did not complete; inspect diagnostics privately.")
WScript.Quit
End If
If (response.StatusCode <> 200) Then
outFile.WriteLine("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Invalid JSON response.")
WScript.Quit
End If
success = stringCompare.SetString(result.StringOf("recoveryId"))
stringMatch11 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("recoveryId") <> 1) Or (stringMatch11 = 1)) Then
outFile.WriteLine("Missing or invalid recoveryId.")
WScript.Quit
End If
success = stringCompare.SetString(result.StringOf("recoveryId"))
stringMatch12 = stringCompare.ContentsEqual(deletedBase.GetAsString(),1)
If (stringMatch12 <> 1) Then
outFile.WriteLine("Unexpected recovery identifier.")
WScript.Quit
End If
outFile.WriteLine("Recovery ID: " & result.StringOf("recoveryId"))
If (config.JsonTypeOf("allow_recover") = 5) Then
If (config.BoolOf("allow_recover") = 1) Then
set recoverUrl = CreateObject("Chilkat.StringBuilder")
success = recoverUrl.Append(deletedBase.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = recoverUrl.Append("/recover?api-version=2026-04-01")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Token is expired or near expiration. Obtain a new token before continuing.")
WScript.Quit
End If
request.HttpVerb = "POST"
success = http.HttpReq(recoverUrl.GetAsString(),request,response)
If (success <> 1) Then
outFile.WriteLine("Request did not complete; inspect diagnostics privately.")
WScript.Quit
End If
If (response.StatusCode <> 200) Then
outFile.WriteLine("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Invalid JSON response.")
WScript.Quit
End If
outFile.WriteLine("Recovery accepted; verify availability before resuming consumers.")
End If
End If
outFile.WriteLine("Example completed.")
outFile.Close