Azure / Azure/azure-cli

az CLI returns bad request when using bash variable

Open
#28,159 6 comments 2 reactions 0 assignees View on GitHub
act-observability-squad Auto-Assign Auto-Resolve customer-reported question Recovery Services Backup Service Attention
Dominant language
Python
Stars
4.6k
Forks
3.5k
Avg merge
3d 2h
Merged PRs (30d)
60

Description

### Describe the bug

I have this Az cli command that enables backup for a VM that I'd like to use with a script:

```
#Get required VM ID
getID=$(az vm show\
--resource-group $resourceGroup \
--name $vmName \
--query id | tr -d '"')

# Enable backup for VM with the created policy
az backup protection enable-for-vm \
--resource-group $resourceGroup \
--vault-name $vaultName \
--vm $getID \
--policy-name $policyName
```
i get this error:

`Operation returned an invalid status 'Bad Request'`

this is the official Azure documentation i followed :

`az backup protection enable-for-vm --resource-group myResourceGroup --vault-name myRecoveryServicesVault --vm $(az vm show -g VMResourceGroup -n MyVm --query id | tr -d '"') --policy-name DefaultPolicy
`
when I run the same command substituting the $getID with its value (echo $getID and then copy paste the value into the command) it works perfectly:
```

az backup protection enable-for-vm \
--resource-group $resourceGroup \
--vault-name $vaultName \
--vm /subscriptions/60793921-aaaaa-wwwww-ffff-xxxxxxxxxxxxxx/resourceGroups/rg_vm_backup/providers/Microsoft.Compute/virtualMachines/backup-vm \
--policy-name $policyName
```

### Related command

az backup protection enable-for-vm

### Errors

`Operation returned an invalid status 'Bad Request'`

### Issue script & Debug output

raceback (most recent call last):
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py", line 233, in invoke
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 663, in execute
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 726, in _run_jobs_serially
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 718, in _run_job
File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/backup/_exception_handler.py", line 10, in backup_exception_handler
azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'Bad Request'

cli.azure.cli.core.azclierror: Operation returned an invalid status 'Bad Request'
az_command_data_logger: Operation returned an invalid status 'Bad Request'
cli.knack.cli: Event: Cli.PostExecute []
az_command_data_logger: exit code: 1
cli.__main__: Command ran in 1.359 seconds (init: 0.561, invoke: 0.798)

### Expected behavior

VM backup should be enabled for the VM

### Environment Summary

azure-cli 2.53.0 *

core 2.53.0 *
telemetry 1.1.0

Dependencies:
msal 1.24.0b2
azure-mgmt-resource 23.1.0b2

Python location 'C:\Program Files\Microsoft SDKs\Azure\CLI2\python.exe'
Extensions directory 'C:\Users\HENDAOUIMohamedAli\.azure\cliextensions'

Python (Windows) 3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)]

Legal docs and information: aka.ms/AzureCliLegal

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.