Hi,
One of the Azure CLI commands in this page isn't working after modifying it to suit my environment.
Azure CLI
az backup protection enable-for-vm --resource-group {ResourceGroup} --vault-name {vaultname} --vm $(az vm show -g VMResourceGroup -n MyVm --query id | tr -d '"') --policy-name {policyname} --disk-list-setting include --diskslist {LUN number(s) separated by space}
The below
--query id | tr -d '"'
seems to be what's causing the issue.
Changing that to
--query id -o json
seems to work just fine.
Thanks,
David
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@dcurwin Thanks for the quick response.
@david-stewart-palapanou Thanks for your comment! We will review and provide an update as appropriate.
@pvrk - FYI
Working with CLI team on this.
This is weird, --query id will print the ID in JSON format, thus surrounded by double quotes ":
# Use 'name' as a simpler example
$ az group show -n rg1 --query name
"rg1"
Removing | tr -d '"' will actually cause the command to fail:
$ az vm list -g $(az group show -n rg1 --query name --output json) --debug
Command arguments: ['vm', 'list', '-g', '"rg1"', '--debug']
Resource group '"rg1"' could not be found.
| tr -d '"' is for stripping the double quotes:
$ az group show -n rg1 --query name | tr -d '"'
rg1
$ az group show -n $(az group show -n rg1 --query name | tr -d '"')
{
"id": "/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/rg1",
"location": "westus",
"managedBy": null,
"name": "rg1",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
What shell are you using? Could you share the output when your run the command inside $()?
az vm show -g VMResourceGroup -n MyVm --query id
az vm show -g VMResourceGroup -n MyVm --query id | tr -d '"'
BTW, Azure CLI has its own way to strip double quotes with the --output parameter:
$ az group show -n rg1 --query name --output tsv
rg1
I'm using Azure CLI (locally).
When running the below command:
$(az vm show -g testrg -n testvm --query id | tr -d '"')
This provides me the following output:
tr : The term 'tr' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:50
+ $(az vm show -g testrg -n testvm --query id | tr -d '"')
+ ~~
+ CategoryInfo : ObjectNotFound: (tr:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
When running the below command:
az vm show -g testrg -n testvm --query id
I receive the subscription ID in response:
"/subscriptions/xxxxxxxxxxxxxxxxxxxxx/resourceGroups/testrg/providers/Microsoft.Compute/virtualMachines/testvm"
I got it. You are using PowerShell. tr is a Linux command which is not available in PoewrShell, you may use --output tsv instead, which will strip the double quotes.
az group show -n $(az group show -n rg1 --query name) happens to work in PowerShell because double quotes are not escaped, and parsed by CMD. For more info, see https://github.com/Azure/azure-cli/blob/dev/doc/quoting-issues-with-powershell.md
Thank you for confirming. I hadn't realised that tr was a Linux command. I certainly didn't recognise it in any Windows commands I've run previously, but I struggled to find references to the command when searching online. Now I've realised all it does it apply a translation to the output of --query id which deletes the double quote character symbol by using the delete -d '"' option and specifying the character to remove. Thank you for the explanation.
As a suggestion, perhaps the documentation can be updated to include an explanation that the command will work for Linux, and referring to the --output tsv option for when running the command on Windows?
@david-stewart-palapanou : Since "--output tsv" works for both (Windows and Linux), the document is already modified and we removed the 'TR' part. Hope this is enough and we can close this issue.
@david-stewart-palapanou : Since "--output tsv" works for both (Windows and Linux), the document is already modified and we removed the 'TR' part. Hope this is enough and we can close this issue.
Thank you @pvrk - yes that's great. Please go ahead and close this issue.
Thanks David. Closing the issue.
@david-stewart-palapanou, thanks for the feedback. I summitted PR https://github.com/MicrosoftDocs/azure-docs/pull/63197 to use --output tsv instead as a cross-platform solution.
The document Configure backup with Azure CLI is now showing the latest version.
Most helpful comment
@david-stewart-palapanou : Since "--output tsv" works for both (Windows and Linux), the document is already modified and we removed the 'TR' part. Hope this is enough and we can close this issue.