Azure CLI echo command not responding - azure

I'm looking to store my connection string in the following variable:
connectionString= az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv
When I execute the above, I get the full connection string in the response. However, when I input:
echo $connectionString
...I get a blank response. The variable is not being stored. Any recommendations on what else to try?

You could use command substitution in order to capture the output in a variable:
connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)
If you need to preserve output across multiple lines, i.E. when the Azure CLI returns values in JSON format, you may want to use a slightly different format for the output to stdout.
Consider this example:
varResourceGroup=$(az group show -n $resourceGroup)
Using the same command as in your example for the output to stdout would result in a single line:
echo $varResourceGroup 
{ "id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>", "location": "westeurope", "managedBy": null, "name": "<resourceGroup_name>", "properties": { "provisioningState": "Succeeded" }, "tags": null }
If you use a sightly different format, the line breaks are preserved:
echo "$varResourceGroup"
{
  "id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>",
  "location": "westeurope",
  "managedBy": null,
  "name": "<resourceGroup_name>",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null
}

As Holger said, we can use this script to define the variable:
connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)
Also, we can use this way to define this variable, like this:
[root#jasoncli#jasonye ~]# connectionstring=`az storage account show-connection-string -n jasondisk3 -g jasonauto --query connectionString -o tsv`
[root#jasoncli#jasonye ~]# echo $connectionstring
DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=jasondisk3;AccountKey=m+kQwLuQZiI3LMoMTyAI8KxxxxD+ZaT9HUL3Agxxxxqul4s8fAIHGPMTD/AG2j+TPHBpttq5hXRmTaQ==
Hope this helps.

Related

How to assign json file's content to a variable using powershell

I trying to run below PowerShell script to add azure data factory data sets. But im getting motioned error.
Json File
{
"name": "DSNAME",
"properties": {
"linkedServiceName": {
"referenceName": "REGNAME1",
"type": "LinkedServiceReference"
},
"annotations": [],
"type": "AzureDataExplorerTable",
"schema": [],
"typeProperties": {
"table": "TABLE_TEST"
}
},
"type": "Microsoft.DataFactory/factories/datasets"
}
Powershell
az config set extension.use_dynamic_install=yes_without_prompt
Get-ChildItem "ADF_DATASETS/" -Filter *.json |
Foreach-Object {
$content = Get-Content $_.FullName
az datafactory dataset create --properties $content --name "DATASETNAME" --factory-name "ADFNAME" --resource-group "RG_TEST"
}
Error:
Error detail: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Please provide a valid JSON file path or JSON string.
The provided JSON string may have been parsed by the shell. See https://docs.microsoft.com/cli/azure/use-cli-effectively#use-quotation-marks-in-arguments
ERROR: Failed to parse string as JSON:
What kind of text --properties expect?
It is expecting JSON string in double quotation marks. So you have to put the value of $content inside the double quotation mark.
az datafactory dataset create --properties "{\"type\":\"AzureBlob\",\"linkedServiceName\":{\"type\":\"LinkedServiceReference\",\"referenceName\":\"exampleLinkedService\"},\"parameters\":{\"MyFileName\":{\"type\":\"String\"},\"MyFolderPath\":{\"type\":\"String\"}},\"typeProperties\":{\"format\":{\"type\":\"TextFormat\"},\"fileName\":{\"type\":\"Expression\",\"value\":\"#dataset().MyFileName\"},\"folderPath\":{\"type\":\"Expression\",\"value\":\"#dataset().MyFolderPath\"}}}" --name "exampleDataset" --factory-name "exampleFactoryName" --resource-group "exampleResourceGroup"

Powershell - Retrieve values from Azure (az) commands

I'm trying to write a simple script to retrieve a key value from Azure Storage account. Below is the code:
az login
az account set --subscription 1111-222-3333-444-55555555
$azure_blob_login = "teststorage123"
$azure_blob_access_key = az storage account keys list --account-name $azure_blob_login
Write-Output $azure_blob_access_key
Write-Output $azure_blob_access_key[0]
The output from the first write-output is:
[
{
"creationTime": "2022-02-23T06:45:00.085500+00:00",
"keyName": "key1",
"permissions": "FULL",
"value": "11111111111111111111111111111111111111111111111"
},
{
"creationTime": "2022-02-23T06:45:00.085500+00:00",
"keyName": "key2",
"permissions": "FULL",
"value": "22222222222222222222222222222222222222222222"
}
]
However the output from the second write-output where I am trying to index to just get the first value in the array is just:
[
Anyone know how I can return the value from this az command and only get the first "value" entry from the first entry in the array?
Thanks,
The default output from Az cli is JSON.
If you want to create an array of properties you would have to convert the az cli output to a psobject
$keys = $azure_blob_access_key | ConvertFrom-Json
$keys[0]

How to change LinuxFxVersion in Azure function app

i have azure function with LinuxFxVersion set to DOTNET:
"siteProperties": {
"metadata": null,
"properties": [
{
"name": "LinuxFxVersion",
"value": "DOTNET|3.1"
},
{
"name": "WindowsFxVersion",
"value": null
}
],
"appSettings": null
},
I want to set it to Python:
"siteProperties": {
"metadata": null,
"properties": [
{
"name": "LinuxFxVersion",
"value": "Python|3.9"
},
{
"name": "WindowsFxVersion",
"value": null
}
],
"appSettings": null
},
According to msdn source, I need to use Power shell to change it:
az functionapp config set --name <func_name> --resource-group <rg> --linux-fx-version 'Python|3.9'
but im getting error:
'3.9' is not recognized as an internal or external command,
operable program or batch file.
When im typing just 'Python' i get response:
Operation returned an invalid status 'Bad Request'
How to change linux fx version in Azure Function from .NET to Python?
The way you can solve this error in Powershell is to wrap up the string containing the pipe character with quotes.
Here are multiple examples:
az functionapp config set --name <func_name> --resource-group <rg> --linux-fx-version '"Python|3.9"'
az functionapp config set --name <func_name> --resource-group <rg> --linux-fx-version 'Python"|"3.9'
If you are running the above command in bash use : instead of |
az functionapp config set --name <func_name> --resource-group <rg> --linux-fx-version "Python:3.9"
https://octopus.com/blog/powershell-pipe-escaping
https://github.com/Azure/azure-cli/issues/7874

How to do this query (az --query "custom-headers host")

I can't just print the (value) information.
],
"id": "/subscriptions/x/resourceGroups/x/providers/Microsoft.Network/trafficManagerProfiles/x",
"location": "global",
"maxReturn": null,
"monitorConfig": {
"customHeaders": [
{
"name": "host",
"value": "site.company.com"
}
],
az network traffic-manager profile list -g X --output table --query "[].{Traffic:name, URL:id.monitorConfig.customHeaders.value}"
You can pull the traffic manager name, customer headers values using the below Azure CLI query in Json format:
az network traffic-manager profile list --resource-group stacklogictest --query "[].{name:name,url:monitorConfig.customHeaders[].value}" --output json

While writing the command to get the storage account key.There will be two keys,how to store a value in variable

az storage account keys list -g <resourcegroupname> -n <accountname>
[
{
"keyName": "key1",
"permissions": "Full",
"value": "<key1value>=="
},
{
"keyName": "key2",
"permissions": "Full",
"value": "<key2value>=="
}
]
--query is used to execute a JMESPath query on the results of commands. The --query argument is supported by all commands in the Azure CLI. You could get properties in an array by this.
key=$(az storage account keys list -g <resourcegroupname> -n <accountname> --query '[<index>].<parameter>')
echo $key
To extract the first key and store it in a variable, you can use the following command:
var=$(az storage account keys list -g rgname -n storagAccName -o json --query "[0].value")

Resources