Search code examples
azure-powershellazure-redis-cache

How can I toggle public network access for azure redis cache using powershell?


How can I toggle public network access for azure redis cache using powershell?

I have tried figuring out a way using Set-AzRedisCache and Get-AzResource through properties and through az resource update, but I can't seem to get to the property or I'm missing something.

EDIT: In case someone comes here and can't use CLI from the answer for whatever reason, I will briefly mention that I ran into this during my explorations: It is also possible to use a PATCH call like this with Invoke-AzRestMethod (but adapt to correct resource type). https://learn.microsoft.com/en-us/powershell/azure/manage-azure-resources-invoke-azrestmethod?view=azps-12.3.0#using-invoke-azrestmethod-with-patch-operations


Solution

  • AFAIK, there is no parameter to disable or enable Azure Redis Cache public network access using Azure PowerShell, but you can do this using Azure CLI.

    Powershell Command:

    $redisCaches = Get-AzRedisCache -ResourceGroupName $resourceGroupName -Name $redisCacheName
    

    enter image description here

    Here is the configuration of my Redis Cache before the update.

    $redisCache = az redis show --name "demp-cache" --resource-group "Demo-RG"
    

    enter image description here

    To enable public network access, you can use the CLI command below

    $redisCache = az redis update --name "demp-cache" --resource-group "Demo-RG" --set publicNetworkAccess=Enabled
    

    Output:

    enter image description here

    After completing the provisioning, the public network access has been changed to the Enabled state.

    enter image description here

    To toggle public network access for Azure Redis Cache, you can use below code.

    $resourceGroupName = "Demo-RG"
    $redisCacheName = "demp-cache"
    
    $currentAccess = az redis show --name "$redisCacheName" --resource-group "$resourceGroupName" --query "publicNetworkAccess" -o tsv
    
    if ($currentAccess -eq "Enabled") {
        
        Write-Host "Public network access is now Enabled."
        az redis update --name "$redisCacheName" --resource-group "$resourceGroupName" --set publicNetworkAccess=Disabled
        Write-Host "Public network access is now disabled."
    } else {
       
        az redis update --name "$redisCacheName" --resource-group "$resourceGroupName" --set publicNetworkAccess=Enabled
        Write-Host "Public network access is now enabled."
    }
    

    Output:

    enter image description here

    Reference: az redis update