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

Azure Storage Accounts: Configure Storage Firewall IP and Subnet Rules

See more Azure Storage Accounts Examples

Reads and preserves existing network ACLs, adds an IP rule and optionally a subnet rule, then prepares publicNetworkAccess=Enabled with defaultAction=Deny. Replace the documentation IP with a real public outbound IPv4 address. A subnet must already have the appropriate Storage service endpoint.

Review the target and set applyChanges=true to submit. This can interrupt clients and can reopen a disabled public endpoint for selected sources. Existing bypass exceptions and resource-instance rules remain in effect. These rules do not create private endpoints, configure DNS, or grant data permissions. Coordinate concurrent network-policy writers.

See Storage firewall and network access for same-region Azure traffic, service endpoints, private networking, and service-specific limitations.

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

$oStringCompare = ObjCreate("Chilkat.StringBuilder")
;  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.
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/azureToken.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load the private Azure Resource Manager token file." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("A nonempty access_token string is required." & @CRLF)
    Exit
EndIf

;  Use your subscription ID as a string. The all-zero value is only a placeholder.
Local $subscriptionId = "00000000-0000-0000-0000-000000000000"
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.Accept = "application/json"
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 30
$oHttp.ReadTimeout = 60
$bSuccess = $oHttp.SetUrlVar("subscriptionId",$subscriptionId)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode subscription ID." & @CRLF)
    Exit
EndIf

$oResponse = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
$oParsed = ObjCreate("Chilkat.Url")
Local $sResourceGroupName = "my-resource-group"
$bSuccess = $oHttp.SetUrlVar("resourceGroupName",$sResourceGroupName)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode resource group." & @CRLF)
    Exit
EndIf

Local $sAccountName = "youruniquestorageaccount"
$bSuccess = $oHttp.SetUrlVar("accountName",$sAccountName)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode account name." & @CRLF)
    Exit
EndIf

Local $sResourceUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01"
$bSuccess = $oHttp.HttpNoBody("GET",$sResourceUrl,$oResponse)
If ($oResponse.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Inspect LastErrorText privately; it may contain credentials." & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResponse.GetHeaderField("x-ms-request-id") & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    ;  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.
    Exit
EndIf

If ($bSuccess <> True) Then
    ConsoleWrite("The HTTP response could not be read completely." & @CRLF)
    Exit
EndIf

$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

If ($oResult.JsonTypeOf("properties.networkAcls") <> 3) Then
    ConsoleWrite("Missing or invalid properties.networkAcls." & @CRLF)
    Exit
EndIf

$oNetwork = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oResult.ObjectOf2("properties.networkAcls",$oNetwork)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read network rules." & @CRLF)
    Exit
EndIf

;  Preserve existing exceptions, resource-instance rules, IP rules, and subnet IDs.
;  Replace this documentation address with your client's PUBLIC outbound IPv4.
Local $sAllowedIp = "198.51.100.10"
;  Optional: full ARM subnet ID, kept as a string. Its Storage service endpoint
;  must already be enabled. This example does not create a subnet or endpoint.
Local $subnetId = ""
$oIpRules = ObjCreate("Chilkat.JsonArray")
If ($oNetwork.HasMember("ipRules") <> True) Then
    $oNetwork.UpdateNewArray("ipRules")
EndIf

$bSuccess = $oNetwork.ArrayOf2("ipRules",$oIpRules)
If ($bSuccess <> True) Then
    ConsoleWrite("Invalid IP rule array." & @CRLF)
    Exit
EndIf

$oRule = ObjCreate("Chilkat.JsonObject")
Local $bIpFound = False
Local $i = 0
While ($i < $oIpRules.Size)
    $bSuccess = $oIpRules.ObjectAt2($i,$oRule)
    If ($bSuccess <> True) Then
        ConsoleWrite("Invalid IP rule." & @CRLF)
        Exit
    EndIf

    If ($oRule.StringOfEquals("value",$sAllowedIp,True) = True) Then
        $bIpFound = True
    EndIf

    $i = $i + 1
Wend
If ($bIpFound <> True) Then
    $bSuccess = $oIpRules.AddObjectAt2(-1,$oRule)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not append IP rule." & @CRLF)
        Exit
    EndIf

    $oRule.UpdateString("value",$sAllowedIp)
    $oRule.UpdateString("action","Allow")
EndIf

$oSubnets = ObjCreate("Chilkat.JsonArray")
If ($oNetwork.HasMember("virtualNetworkRules") <> True) Then
    $oNetwork.UpdateNewArray("virtualNetworkRules")
EndIf

$bSuccess = $oNetwork.ArrayOf2("virtualNetworkRules",$oSubnets)
If ($bSuccess <> True) Then
    ConsoleWrite("Invalid subnet rule array." & @CRLF)
    Exit
EndIf

Local $bSubnetFound = False
$i = 0
While ($i < $oSubnets.Size)
    $bSuccess = $oSubnets.ObjectAt2($i,$oRule)
    If ($bSuccess <> True) Then
        ConsoleWrite("Invalid subnet rule." & @CRLF)
        Exit
    EndIf

    ;  state is returned by GET but is not a writable rule property.
    $oRule.Delete("state")
    If ($oRule.StringOfEquals("id",$subnetId,False) = True) Then
        $bSubnetFound = True
    EndIf

    $i = $i + 1
Wend
$oStringCompare.SetString($subnetId)
Local $bStringMatch1 = $oStringCompare.ContentsEqual("",True)
If (($bStringMatch1 <> True) And ($bSubnetFound <> True)) Then
    $bSuccess = $oSubnets.AddObjectAt2(-1,$oRule)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not append subnet rule." & @CRLF)
        Exit
    EndIf

    $oRule.UpdateString("id",$subnetId)
    $oRule.UpdateString("action","Allow")
EndIf

$oNetwork.UpdateString("defaultAction","Deny")
$oRequest = ObjCreate("Chilkat.JsonObject")
$oProperties = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oRequest.AppendObject2("properties",$oProperties)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not create properties." & @CRLF)
    Exit
EndIf

$bSuccess = $oProperties.AppendObjectCopy("networkAcls",$oNetwork)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not copy network rules." & @CRLF)
    Exit
EndIf

;  Enabled + defaultAction Deny means selected public networks. Disabled would
;  block public endpoint access; SecuredByPerimeter is a different policy mode.
$oProperties.UpdateString("publicNetworkAccess","Enabled")
Local $bApplyChanges = False
If ($bApplyChanges <> True) Then
    ConsoleWrite("Network changes prepared, not applied. Review rules and set applyChanges to true." & @CRLF)
    Exit
EndIf

;  This may reopen a previously disabled public endpoint for the selected sources.
;  Preserve exceptions intentionally; preserved trusted-service bypasses still apply.
;  On a write timeout, read the resource before deciding whether to retry.
$bSuccess = $oHttp.HttpJson("PATCH",$sResourceUrl,$oRequest,"application/json",$oResponse)
If ($oResponse.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Inspect LastErrorText privately; it may contain credentials." & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResponse.GetHeaderField("x-ms-request-id") & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    ;  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.
    Exit
EndIf

If ($bSuccess <> True) Then
    ConsoleWrite("The HTTP response could not be read completely." & @CRLF)
    Exit
EndIf

$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

ConsoleWrite("Network policy update returned successfully. Verify access from intended clients." & @CRLF)