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

Azure Storage Accounts: Retrieve and Optionally Rotate a Storage Account Key

See more Azure Storage Accounts Examples

Uses List Keys to retrieve an account key, or regenerates only key1 when rotateKey=true. Saves only the selected secret to a private file and never prints it. The caller needs List Keys permission and, for rotation, Regenerate Key permission.

Regeneration immediately invalidates the old key and SAS signatures derived from it. First migrate all dependent clients to the other key; regenerate the unused key, distribute its new value securely, and only later consider rotating the other key. Do not regenerate both together. A failed local save does not undo rotation; recover using List Keys. Prefer Entra authentication where supported. See account key management.

These calls manage Azure resources at management.azure.com. They do not upload or download stored data. Supply a current Microsoft Entra access token for Azure Resource Manager in private qa_data/tokens/azureToken.json, with a nonempty access_token string. For v2 client credentials, request https://management.azure.com/.default; obtain a new token when it expires. A storage account key, SAS, Microsoft Graph token, or Storage data-plane token is not an ARM token. Never print the token or commit its file.

Azure RBAC must grant the caller the required management actions at the appropriate scope. An app registration alone does not grant resource access. Management permissions and blob/file data permissions are distinct. These examples target public Azure; sovereign clouds require the corresponding authority, ARM endpoint, token audience, and continuation-host validation.

Microsoft API reference

Chilkat VBScript Downloads

VBScript
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

'  This example requires the Chilkat API to have been previously unlocked.
'  See Global.UnlockBundle for sample code.

'  This is Azure Resource Manager (the management plane), not a blob data request.
'  Load an unexpired Microsoft Entra token whose audience is Azure Resource Manager.
'  For the v2 token endpoint, request scope https://management.azure.com/.default.
'  Keep the token file private and out of source control and public web directories.
set token = CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.LoadFile("qa_data/tokens/azureToken.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load the private Azure Resource Manager token file.")
    WScript.Quit
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    outFile.WriteLine("A nonempty access_token string is required.")
    WScript.Quit
End If

'  Use your subscription ID as a string. The all-zero value is only a placeholder.
subscriptionId = "00000000-0000-0000-0000-000000000000"
set http = CreateObject("Chilkat.Http")
http.AuthToken = token.StringOf("access_token")
http.Accept = "application/json"
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 30
http.ReadTimeout = 60
success = http.SetUrlVar("subscriptionId",subscriptionId)
If (success <> 1) Then
    outFile.WriteLine("Could not encode subscription ID.")
    WScript.Quit
End If

set response = CreateObject("Chilkat.HttpResponse")
set result = CreateObject("Chilkat.JsonObject")
result.Strict = 1
set parsed = CreateObject("Chilkat.Url")
resourceGroupName = "my-resource-group"
success = http.SetUrlVar("resourceGroupName",resourceGroupName)
If (success <> 1) Then
    outFile.WriteLine("Could not encode resource group.")
    WScript.Quit
End If

accountName = "youruniquestorageaccount"
success = http.SetUrlVar("accountName",accountName)
If (success <> 1) Then
    outFile.WriteLine("Could not encode account name.")
    WScript.Quit
End If

keysUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/listKeys?api-version=2026-09-01"

success = http.HttpNoBody("POST",keysUrl,response)
If (response.StatusCode = 0) Then
    outFile.WriteLine("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    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"))
    outFile.WriteLine("Retry-After: " & response.GetHeaderField("Retry-After"))
    '  Inspect the error body privately. Do not log tokens, headers, or full responses.
    '  For 401 check the token/audience; for 403 check Azure RBAC and policy.
    '  Honor Retry-After for 429 and use bounded backoff for transient failures.
    WScript.Quit
End If

If (success <> 1) Then
    outFile.WriteLine("The HTTP response could not be read completely.")
    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("keys") <> 4) Then
    outFile.WriteLine("Missing or invalid keys.")
    WScript.Quit
End If

'  Move all clients and key-signed SAS dependencies away from key1 BEFORE rotating it.
'  This example retrieves key1, then optionally regenerates key1 only.
keyName = "key1"
rotateKey = 0
If (rotateKey = 1) Then
    set request = CreateObject("Chilkat.JsonObject")
    success = request.UpdateString("keyName",keyName)
    success = http.HttpJson("POST","https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/regenerateKey?api-version=2026-09-01",request,"application/json",response)

    '  Do not retry regeneration after an uncertain transport result: list keys
    '  to recover the current value. Repeating regeneration invalidates it again.
    If (response.StatusCode = 0) Then
        outFile.WriteLine("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
        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"))
        outFile.WriteLine("Retry-After: " & response.GetHeaderField("Retry-After"))
        '  Inspect the error body privately. Do not log tokens, headers, or full responses.
        '  For 401 check the token/audience; for 403 check Azure RBAC and policy.
        '  Honor Retry-After for 429 and use bounded backoff for transient failures.
        WScript.Quit
    End If

    If (success <> 1) Then
        outFile.WriteLine("The HTTP response could not be read completely.")
        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("keys") <> 4) Then
        outFile.WriteLine("Missing or invalid keys.")
        WScript.Quit
    End If

End If

set keys = CreateObject("Chilkat.JsonArray")
success = result.ArrayOf2("keys",keys)
If (success <> 1) Then
    outFile.WriteLine("Could not read returned keys.")
    WScript.Quit
End If

set key = CreateObject("Chilkat.JsonObject")
set privateKey = CreateObject("Chilkat.JsonObject")
found = 0
i = 0
Do While (i < keys.Size)
    success = keys.ObjectAt2(i,key)
    If (success <> 1) Then
        outFile.WriteLine("Invalid key entry.")
        WScript.Quit
    End If

    If (key.StringOfEquals("keyName",keyName,1) = 1) Then
        If ((key.JsonTypeOf("value") <> 1) Or (key.StringOfEquals("value","",1) = 1)) Then
            outFile.WriteLine("Selected key has no value. Inspect permissions privately.")
            WScript.Quit
        End If

        success = privateKey.UpdateString("account_name",accountName)
        success = privateKey.UpdateString("key_name",keyName)
        success = privateKey.UpdateString("access_key",key.StringOf("value"))
        found = 1
    End If

    i = i + 1
Loop
If (found <> 1) Then
    outFile.WriteLine("Selected key was not returned.")
    WScript.Quit
End If

'  Pre-create this private directory with restrictive OS permissions. This file
'  contains an account-wide secret; never commit it or put it in web content.
success = privateKey.WriteFile("qa_data/azure/storage-key.json")
If (success <> 1) Then
    outFile.WriteLine("Could not save key privately. If rotated, use List Keys to recover; do not regenerate again.")
    WScript.Quit
End If

outFile.WriteLine("Selected key saved privately. Key name: " & keyName)
outFile.WriteLine("Key regeneration requested: " & rotateKey)
'  Distribute the current key through your application's secret-management process.
'  Rotate the other key in a separate maintenance step after clients are migrated.

outFile.Close