Powershell - Retrieve values from Azure (az) commands - azure

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]

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"

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")

Azure App Configuration: Getting PrimaryKey in a arm template

I have extracted the ARM template belonging to the preview version of Azure App Configuration, and am setting it into our IaC repository - so far so good.
Our next logical step is to include insertion of the AppConfiguration.PrimaryKey into our Key Vault. However I do not know the name of this property, and I can not find any information on the subject online. Also I can not see the AppConfiguration/configurationStores type listed in resources.azure.com (assuming its because its still in public preview).
Does anyone know how to reference the primary key (and possibly the read-only primary key), so i can reference them through a "outputs" variable in my arm template?
Then I can let Az Cli/Az Powershell insert the secret into our Key Vault, and we obtain full automation of our IaC
I was not able to figure this out.
However by using az cli commands in a IaC script (which anyways invokes the arm template residing in a azure blob store) I circumvented the problem:
$connStrings = az appconfig credential list -n $configName| ConvertFrom-Json
$readOnlyConnString = ($connStrings | Where {$_.name -eq "Primary Read Only"}).connectionString
$primaryConnString = ($connStrings | Where {$_.name -eq "Primary"}).connectionString
#then
az keyvault secret set --vault-name $kvName --name $keyNameRO --value $readOnlyConnString
az keyvault secret set --vault-name $kvName --name $keyNamePrimary --value $primaryConnString
For an ARM template I did the following. The listkeys function returns a full list of all the values that have to do with the keys. This was hard to figure out. I hope it helps.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"configurationStores_instance_name": {
"defaultValue": "ac-instance",
"type": "String"
}
},
"variables": {
"apiVersionVar": "[providers('Microsoft.AppConfiguration', 'configurationStores').apiVersions[0]]",
"resourceId": "[resourceId('Microsoft.AppConfiguration/configurationStores', parameters('configurationStores_instance_name'))]",
},
"resources": [
{
"type": "Microsoft.AppConfiguration/configurationStores",
"apiVersion": "2019-10-01",
"name": "[parameters('configurationStores_instance_name')]",
"location": "northcentralus",
"sku": {
"name": "standard"
},
"properties": {}
}
],
"outputs": {
"AppConfigEndpoint": {
"type": "string",
"value": "[reference(parameters('configurationStores_instance_name')).endpoint]"
},
"AppConfigKeys": {
"type": "Array",
"value": "[listkeys(variables('resourceId'), variables('apiVersionVar')).value]"
}
}
}
hope this helps!

How to get nested properties using Azure CLI

I had been using the line below to grab the value of the internalIpAddress property from an ILB App Service Environment in Azure:
az resource show `
--ids "/subscriptions/$subscription_id/resourceGroups/$ilbase_rg_name/providers/Microsoft.Web/hostingEnvironments/$ilbase_name/capacities/virtualip" `
--query "internalIpAddress"
The format of the virtualip resource was:
{
"internalIpAddress": "10.30.0.139",
"outboundIpAddresses": [
"13.72.76.135"
],
"serviceIpAddress": "13.72.76.135",
"vipMappings": []
}
Seems like in the past day or so, the format of the virtualip resource has now changed to this:
{
"additionalProperties": {
"internalIpAddress": "10.30.0.139",
"outboundIpAddresses": [
"13.72.76.135"
],
"serviceIpAddress": "13.72.76.135",
"vipMappings": []
},
"id": null,
"identity": null,
"kind": null,
"location": null,
"managedBy": null,
"name": null,
"plan": null,
"properties": null,
"sku": null,
"tags": null,
"type": null
}
And now my command no longer works...it returns nothing. I can modify my command to get the entire additionalProperties object but I then don't know how to parse thru it to get just the value of the internalIpAddress property.
Another interesting note on this is, if you go to the Azure Resource Explorer and navigate to the virtualip resource, it still shows it in the same old format. If you try the PowerShell code the Azure Resource Explorer gives you to query the resource, it returns nothing.
Here is the PowerShell the Azure Resource Explorer said to use:
Get-AzureRmResource -ResourceGroupName MyRG -ResourceType Microsoft.Web/hostingEnvironments/capacities -ResourceName "myilbase/virtualip" -ApiVersion 2018-02-01
Looking for some help on how to parse the nested internalIpAddress property from the additionalProperties object
just traverse the object like you normally would:
--query "additionalProperties.internalIpAddress"

Resources