Skip to content

az CLI returns bad request when using bash variable #28159

@DaliBTGHendaoui

Description

@DaliBTGHendaoui

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 [<function AzCliLogging.deinit_cmd_metadata_logging at 0x000002031E5E0700>]
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Auto-AssignAuto assign by botAuto-ResolveAuto resolve by botRecovery Services Backupaz backupService AttentionThis issue is responsible by Azure service team.act-observability-squadcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.questionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions