Search code examples
azureservicecommand-line-interfaceendpoint

az update service-endpoints using variable


I'm trying to add multiple Service-Endpoints to subnets using the update command, and having a variable to represent the SE's

When it runs, it fails with and error saying that the array is using an invalid service name.

When running the command without a variable for the SE's, it runs with out any problem.

$SE = "Microsoft.KeyVault Microsoft.Storage"

az network vnet subnet update --service-endpoints $SE --resource-group MyRg1 --vnet-name MyVnet --name MySnet

## Used to display the varaible format
Write-host "az network vnet subnet update --service-endpoints $SE --resource-group MyRg1 --vnet-name MyVnet --name MySnet"

Using a loop, and adding each SE is not a good option, as the update cmd is idempotent.


Solution

  • This has to do with how powershell handles variables; the $SE that you pass in is a single positional parameter, whereas the az client parses them as being distinct. This is a common problem with powershell. For example, consider an application that prints the command line arguments:

    > $SE = "my args"
    > MyExe.exe $SE something else
    

    The output would be:

    arg0: MyExe.exe
    arg1: my args
    arg2: something
    arg3: else
    

    To correct this, you need to instruct powershell to split the string into an array, which (when it builds a commandline for az) gets split into multiple args:

    az network vnet subnet update --service-endpoints $($SE -split ' ') --resource-group MyRg1 --vnet-name MyVnet --name MySnet