Manipulating output return from az cli command in powershell or azcli - azure

I ran the following AZ cli command below, whilst it provided me with the information I was after, I could not filter or select the columns I wanted.
az pipelines list --org org_url --project az_project --out table
I am after the name and status column only, and would like to filter on where the name matches the string "dev"
Another option i looked at was piping this out in Powershell
$output = az pipelines list --org org_url --project az_project --out table
$output | select-object name, status
I found that this didnt work either, as looking at the members of $output suggests it was not converted to a custom object. I am thinking there must be a way to do this in powershell.

Credit to #AdminOfThings
This is what I was trying to do.
az pipelines list --org org_url --project az_project | ConvertFrom-Json -Depth 10 | Select Name,Status | where {$_.Name -like "*name*"}

Related

Execute azure command in powrershell without writing error to console?

I am using powershell script in pipeline and the problem I have with this query.
$value = $(az appconfig kv show -n ThisisEnv --key thisisconfigkey) | ConvertFrom-Json
What this query does is get the data related to key if exist. If this key doesn't exist it give the error like
ERROR: Key 'abcdefg' with label 'None' does not exist.
It is work as expected. In pipeline when the key doesn't exist, it's printed a error on CLI. The pipeline see it as error and show it as failed. Is there a way I can make it work.
Is there a way I can stop it printing it on console. Any powershell operator which help me to get the value from azure command but also let me get it without print anything on console.
You could try to redirect the standard error using 2> $null
$value = $(az appconfig kv show -n ThisisEnv --key thisisconfigkey 2> $null) | ConvertFrom-Json
This will suppress the error within the console. You might also want to set the powerShellIgnoreLASTEXITCODE within the Azure CLI Task in order that the pipeline run doesn't fail - or as a workaround, set the $LASTEXITCODE to 0

Get KeyVault Get Access Policies via Powershell AZ CLI

I am trying to read in the access policies using the az cmdlets but struggling to do this. I thought this would work
$foo = az keyvault show -g "my-rg" -n "my-kv"
This returns a value back and was hoping to do:
$accessPolicies = $foo.accessPolicies
However this returns a null. I did notice the output produces properties so I also tried
$accessPolicies = $foo.properties.accessPolicies
Clearly doing something wrong here. Any ideas how I do this?
You could directly query the accessPolicies properties from the Azure CLI command.
$accessPolicies = az keyvault show -g "my-rg" -n "my-kv" --query 'properties.accessPolicies'
The --query parameter needs the JMESPath query string. See http://jmespath.org/ for more information and examples.
If you want to use the PowerShell commands, you can do it like this:
$accessPolicies= (Get-AzKeyVault -vaultname "my-kv" -resourcegroupname "my-rg").AccessPolicies

How to export multiple Azure DevOps Variable Groups?

We have multiple variable groups in our project and I am trying to find ways how I can export them to my local desktop.
So far I have this az script that exports a variable group but I can only do one at a time. I have several variables and I am trying to do all of them at once. Could someone please help on how I can export multiple variables and use the same name of that variable on Azure DevOps to match it when it's saved on the desktop?
az pipelines variable-group show --group-id "id number" --org "https://dev.azure.com/Organization" -p "Project" --output json > test.json
Also when I use the script, the value for secret is showing as "null" instead of "false"? Any reason why it's doing that?
You can use below command to export multiple variable groups at a time. See: az pipelines variable-group for details.
az pipelines variable-group list --org "https://dev.azure.com/Organization" --project "Project" --output json>test.json
In addition, this secret variable is encrypted in variable group by reference to this doc: Add & use variable groups, thus we can only get "null" from return result, which is by design. If you want to use secret variables in Azure Pipelines, please see: Set secret variables for guidance.
To download variable groups in different json file, I've created a script that collects all variable groups, gets their names, and creates folder and files for each variable group. It will create a subfolder VariableGroups in current folder.
$ProjectName = "projectName"
$Organization = "https://dev.azure.com/organizationName"
# Get variable-groups list
$vars = az pipelines variable-group list --organization $Organization --project $ProjectName
# Get variable-groups names list
$v = $vars | ConvertFrom-Json -AsHashtable
$variable_group_names = $v.name
# Go though every variable-group
foreach ($variable_group_name in $variable_group_names) {
$group = az pipelines variable-group list --organization $Organization --project $ProjectName --group-name $variable_group_name
# Get every variable-group Id
$g = $group | ConvertFrom-Json -AsHashtable
$groupId = $g.id
# Check if VariableGroups folder exist, if not - create it
if (!(Test-Path .\VariableGroups)){
New-Item -itemType Directory -Path .\VariableGroups
}
# Get every variable-group content and place it in corresponding folder
az pipelines variable-group show --organization $Organization --project $ProjectName --id $groupId --output json>.\VariableGroups\$variable_group_name.json
Write-Host created .\VariableGroups\$variable_group_name.json
}

How to output the format into a file in powershell/azure

I`m trying to get my VMs from a subscription so I can then create some commands in python to extract different things from the fabric but to do this the easiest way I would need the file to be saved in a CSV format and each time I try to export the content using powershell its not working.
screenshot of powershell results
Can you please let me know how this command should be written?
az vm list --query "[].[resourceGroup,name,storageProfile.osDisk.osType]" -o table | Export-Csv D:\vmlist.csv -Delimiter ";" -Force -NoTypeInformation
According to my research, when we use the command "Export-Csv" to save output as a CSV file, it will treat the output as an object and stores the properties of that object as columns to CSV file. Now the output of the command az vm list --query "[].[resourceGroup,name,storageProfile.osDisk.osType]" -o table is String. So we cannot create CSV file according to your need.
So if you want to create csv file according to your need, please refer to the following script
$result = az vm list --query "[].{resource:resourceGroup, name:name}" -o json | ConvertFrom-Json
$result | export-csv -Path e:\test.csv -NoTypeInformation

Azure CLI choose a option from a table

I am trying to get a table output with numbers in the Azure CLI which gives this as a output
Number Location Name
---------- ----------- -------------
1 somewhere ResourceGroup1
2 somewhere ResourceGroup2
The code I have right now is
az group list --query '[].{location:location, name:name}'
The output I'm getting right now is
Location Name
---------- ---------------
somewhere ResourceGroup1
somewhere ResourceGroup2
My end goal is that if you choose the number 1 you select the name so I can use that later in the script
For your issue, there is no Azure CLI command can achieve it. But you can use a script to let it come true. For example, you can use a shell script:
#!/bin/bash
az group list --query '[].{location: location, name: name}' -o table >> output.txt
# This command just add the line number inside the file, it's optional.
cat -n output.txt >> result.txt
# you can just get the group name with a specific line, the same result with output.txt
awk '{if (NR == line) print $3}' result.txt
Hope this will be helpful.
you can use contains expression (jmespath) in the filter to filter the results:
filter=resource_group_name
filterExpression="[?contains(name, '$filter')].name"
az group list --query "$filterExpression" -o tsv
which is a much better way compared to already present answers.
more reading:
http://jmespath.org/specification.html#filterexpressions
http://jmespath.org/specification.html#built-in-functions
From what i understand you are trying to create variable to use later from output. You do not need to put it in a table first. Using same example you have you could do something like below;
gpname="$(az group list --query [0].name --output tsv)"
az group show -n $gpname
Good Luck.....
Information in Comments::
What you are looking for is more Linux than Azure. I am not a Linux CLI expert but her is a basic script that you can build on.
#!/bin/bash
gpnames="$(az group list --query [].name --output tsv)"
PS3='Select A number: '
select gpname in $gpnames
do
az group show -n $gpname
Done
Hope this helps......

Resources