Azure Monitor Workspace using powershell

Gary Middleton 20 Reputation points
2024-01-29T11:48:04.0633333+00:00

I have created an Azure Monitor workspace using a powershell script that is listed below. By default this creates this with public access enabled. How can I modify this script to disable this - i.e. select the disable public access button, using powershell ? User's image

Script:

# Azure Monitor workspace creation

try {
    # Check if the Azure Monitor workspace already exists
    $existingWorkspace = az monitor account show --resource-group $ResourceGroupName --name $MonitorWorkspace --query name --output tsv

    if (-not $existingWorkspace) {
        # Azure Monitor workspace does not exist, so create it
        az monitor account create --name $MonitorWorkspace --resource-group $ResourceGroupName --location $region --tags "$Tagkey=$Tagvalue"
    }
    else {
        # Azure Monitor workspace already exists
        Write-Host "Azure Monitor workspace '$MonitorWorkspace' already exists in resource group '$ResourceGroupName'."
    }

} catch {
    # Handle errors, e.g., output an error message
    Write-Host "Error occurred: $_"
}   
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,645 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Deepanshukatara-6769 16,565 Reputation points Moderator
    2024-01-29T12:40:25.8633333+00:00

    Hi , To disable public access for an Azure Monitor workspace, you can use the az monitor workspace update command in your PowerShell script. Specifically, you can set the --public-network-access-for-logs parameter to Disabled. Here's how you can modify your script:

    # Azure Monitor workspace creation
    
    try {
        # Check if the Azure Monitor workspace already exists
        $existingWorkspace = az monitor account show --resource-group $ResourceGroupName --name $MonitorWorkspace --query name --output tsv
    
        if (-not $existingWorkspace) {
            # Azure Monitor workspace does not exist, so create it
            az monitor account create --name $MonitorWorkspace --resource-group $ResourceGroupName --location $region --tags "$Tagkey=$Tagvalue"
            
            # Disable public access for the created workspace
            az monitor workspace update --resource-group $ResourceGroupName --name $MonitorWorkspace --public-network-access-for-logs Disabled
        }
        else {
            # Azure Monitor workspace already exists
            Write-Host "Azure Monitor workspace '$MonitorWorkspace' already exists in resource group '$ResourceGroupName'."
        }
    
    } catch {
        # Handle errors, e.g., output an error message
        Write-Host "Error occurred: $_"
    }
    
    
    

    Doc for ref : https://learn.microsoft.com/en-us/cli/azure/monitor/log-analytics/workspace?view=azure-cli-latest Please accept answer if it helped


  2. tbgangav-MSFT 10,426 Reputation points Moderator
    2024-01-29T16:03:08.1866667+00:00

    Hi @Gary Middleton , It should be possible to enable or disable public network access as per az monitor account update. As shown in below screenshot, the set property name is public_network_access but currently I don't know what value we have to provide to disable the public network access. I have tried disabled, Disabled, false, etc. but none works at this point. I will research more internally and let you know as I get to know the right value to be provided. Thought of sharing this information that I have at this point so it might help you to try few options from your end too. User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.