How to update service endpoint in subnet using Azure CLI command

Dattatray Giramkar 0 Reputation points
2023-06-02T10:32:47.8766667+00:00

Hi Team

I am updating a subnet using an Azure CLI command. Below is the code I am using to create/update the subnet based on the 'subnetParams' hash table. However, I am encountering an error. How can I pass multiple values (e.g., 'Microsoft.KeyVault', 'Microsoft.Storage') to the 'service-endpoint' property using the hash table?

User's image

$resourceGroup = "TEST"
$vnetName = "testvnet"
$addressPrefix = "10.0.0.0/16"

# Define an array of subnets
$subnets = @(
    @{
        Name = "Default"
        AddressPrefix = "10.0.2.0/24"
    },
    @{
        Name = "Subnet1"
        AddressPrefix = "10.0.1.0/24"
        Delegations = "Microsoft.Web/serverFarms"
        ServiceEndpoints = @("Microsoft.KeyVault", "Microsoft.Storage")
    }
)

# Check if VNet exists
if (!(az network vnet show -g $resourceGroup -n $vnetName)) {
    Write-Output "Creating VNet..."
    az network vnet create --name $vnetName --resource-group $resourceGroup --address-prefixes $addressPrefix
} else {
    Write-Output "Updating VNet..."
    az network vnet update --name $vnetName --resource-group $resourceGroup --address-prefixes $addressPrefix
}

# Create or update subnets
foreach ($subnet in $subnets) {
    $subnetName = $subnet.Name
    $subnetAddressPrefix = $subnet.AddressPrefix
    $subnetDelegations = $subnet.Delegations
    $subnetServiceEndpoints = $subnet.ServiceEndpoints

    $subnetParams = @(
        "--vnet-name=$($vnetName)"
        "--resource-group=$($resourceGroup)"
        "--name=$($subnetName)"
        "--address-prefixes=$($subnetAddressPrefix)"
    )
    
    if (![string]::IsNullOrEmpty($subnetDelegations)) {
        $subnetParams += "--delegations=$($subnetDelegations)"
    }
    if (![string]::IsNullOrEmpty($subnetServiceEndpoints)) {
        $subnetParams += "--service-endpoints", $($subnetServiceEndpoints)
    }
    
    if (!(az network vnet subnet show -g $resourceGroup --vnet-name $vnetName -n $subnetName)) {
        Write-Output "Creating Subnet $subnetName..."
        az network vnet subnet create @subnetParams
    } else {
        Write-Output "Updating Subnet $subnetName..."
        az network vnet subnet update @subnetParams
    }
}

It works well when I pass like below

az network vnet subnet update @subnetParams --service-endpoints Microsoft.KeyVault Microsoft.Storage
Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,280 questions
{count} votes

1 answer

Sort by: Most helpful
  1. VasimTamboli 4,775 Reputation points
    2023-06-02T16:04:55.8366667+00:00

    To pass multiple values to the --service-endpoints property in the Azure CLI command using the hash table, you can modify the $subnetParams array to include the values individually. Here's an updated version of your code snippet:

    Azure CLI

    $resourceGroup = "TEST"
    $vnetName = "testvnet"
    $addressPrefix = "10.0.0.0/16"
    
    # Define an array of subnets
    $subnets = @(
        @{
            Name = "Default"
            AddressPrefix = "10.0.2.0/24"
        },
        @{
            Name = "Subnet1"
            AddressPrefix = "10.0.1.0/24"
            Delegations = "Microsoft.Web/serverFarms"
            ServiceEndpoints = @("Microsoft.KeyVault", "Microsoft.Storage")
        }
    )
    
    # Check if VNet exists
    if (!(az network vnet show -g $resourceGroup -n $vnetName)) {
        Write-Output "Creating VNet..."
        az network vnet create --name $vnetName --resource-group $resourceGroup --address-prefixes $addressPrefix
    } else {
        Write-Output "Updating VNet..."
        az network vnet update --name $vnetName --resource-group $resourceGroup --address-prefixes $addressPrefix
    }
    
    # Create or update subnets
    foreach ($subnet in $subnets) {
        $subnetName = $subnet.Name
        $subnetAddressPrefix = $subnet.AddressPrefix
        $subnetDelegations = $subnet.Delegations
        $subnetServiceEndpoints = $subnet.ServiceEndpoints
    
        $subnetParams = @(
            "--vnet-name=$vnetName"
            "--resource-group=$resourceGroup"
            "--name=$subnetName"
            "--address-prefixes=$subnetAddressPrefix"
        )
        
        if (![string]::IsNullOrEmpty($subnetDelegations)) {
            $subnetParams += "--delegations=$subnetDelegations"
        }
        if ($subnetServiceEndpoints) {
            foreach ($endpoint in $subnetServiceEndpoints) {
                $subnetParams += "--service-endpoints=$endpoint"
            }
        }
        
        if (!(az network vnet subnet show -g $resourceGroup --vnet-name $vnetName -n $subnetName)) {
            Write-Output "Creating Subnet $subnetName..."
            az network vnet subnet create @subnetParams
        } else {
            Write-Output "Updating Subnet $subnetName..."
            az network vnet subnet update @subnetParams
        }
    }
    
    
    
    

    In this updated code, the $subnetParams array is modified to include each endpoint individually using a loop. The --service-endpoints parameter is added for each endpoint in the $subnetServiceEndpoints array.

    This modification will allow you to pass multiple values to the --service-endpoints property in the Azure CLI command.

    1 person found this answer helpful.