Alerts can be listed like that with az cli like that
$activities = az monitor activity-log list -g $ResourceGroup
which produces in PowerShell a string but is a list of JSON's.
Any one knows why $activities is not a PSCustomObject which I could use ?
I do agree with #Mathias R. Jessen that
az is an executable and executables return strings, not objects
$activities = az monitor activity-log list -g "Resourcegroup"
$res=$activities | ConvertFrom-Json
$res
Output:
Now you can use Dot Operator as below:
Related
I have multiple azure tenants, each having multiple subscriptions, and I have to run a single PowerShell script for all my subscriptions.
This can be achieved using Azure CLI, and it works perfectly.
I use Azure CLI as below;
$az_account = (az account list --query "[].[name]" -o tsv)
foreach ($account in $az_account) {
az account set --name $account
#<RUN SCRIPTS HERE>#
}
But in some situations, I have to use the Az PowerShell command instead of Azure CLI.
So could anyone help me
How to run Az PowerShell commands for multiple subscriptions
Or the Az PowerShell profile file path ( same as Azure CLI which is C:\Users\%USER\.Azure\azureProfile.json ).
How to run Az PowerShell commands for multiple subscriptions
You can use the below PowerShell Scripts to run the multiple subscription PowerShell commands.
# Get the Subscription Details using Get-AzSubscription Command
Get-AzSubscription | ForEach-Object {
# Set the context Details using Set-AzContext Command which is equalent to the az account set CLI Command
$_ | Set-AzContext
$subscriptionName = $_.Name
#<RUN YOUR SCRIPTS HERE>#
}
Or the Az PowerShell profile file path ( same as Azure CLI which is C:\Users\%USER\.Azure\azureProfile.json ).
Refer here for profile file path location
Resolution
After a couple of tries, finally found some methods to solve my issue. posting the same here might be helpful for someone.
You can connect all the subscriptions using the below commands
Connect-AzAccount -Tenant "xxxx-xxxxx-xxxx-xxx" -Subscription "xxxx-xxxxx-xxxx-xxx"
To List all the connected subscriptions
Get-AzContext -ListAvailable | Select-Object Name, Subscription, Tenant
If you want to rename to a friendly Name,
Rename-AzContext -SourceName "Azure subscription 1 (xxxxx-xxxx-xxxx-xxxx-xxxx) - xxxxx-xxxx-xxx-xxxx-xxxx-xxx - jawad#xyz.com" -TargetName "MySubscription"
To save these profiles to file,
Save-AzContext -Path "C:\Users\jawad\Downloads\AzpwshProfile.json"
You can import any time these profiles using the below command, (test first clear the profile Clear-AzContext -Force)
Import-AzContext -Path "C:\Users\jawad\Downloads\AzpwshProfile.json"
Now you easily use for loop to set the subscriptions, for example
$acc = (Get-AzContext -ListAvailable | Select-Object Name)
foreach ($account in $acc) {
>> Select-AzContext $account.Name
>> Get-AzVM | Export-Csv -Path "inventory.csv"
>> }
Thank you
I'm trying to create multiple values using "az apim nv create" command using loop, but its not working.
with the single command (without using variable in --value) we can able to create, but the same is not working when we use variable in --value.
demo="fromkey fromkey1"
for list in $demo
do
az apim nv create --service-name ABC -g XYZ --secret true --named-value-id $list --display-name $list --value $list
done
Can someone please help on this.
Azure DevOps (az apim nv create) cant able to create variable dynamically
You could try below scripts in the bash task:
- bash: |
demo=( "fromkey" "fromkey1" )
for list in ${demo[#]}; do
az apim nv create --service-name ABC -g XYZ --secret true --named-value-id $list --display-name $list --value $list
done
displayName: 'az apim nv create'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
Update:
I this can be possible with Shell script ? if Yes then can you please
share shell script commands
declare -a demo=("fromkey1" "fromkey2" "fromkey3")
for list in "${demo[#]}"
do
az apim nv create --service-name ABC -g XYZ --secret true --named-value-id $list --display-name $list --value $list
done
I can able to get the resource details by using the tag using the Azure CLI command
az resource list --tag AppID=XXXX --query [].name
However, how can filter resources use more than one tag? Could you please help?
Example:
az resource list --tag AppID=XXXX, Region=DEV --query [].name
Based on the above requirement we have created a script using both Azure CLI cmdlets & PowerShell cmdlet to filter the resources using more than one Tag.
Script using PowerShell Cmdlet:
connect-azaccount
$resource = Get-AzResource -ResourceGroupName <resourcegroupName> -TagName env -TagValue prod |Select-Object -Property ResourceId
$resourcearray=$resource
foreach ( $resource in $resourcearray){
$Tagvalue=(Get-AzTag -ResourceId $resource.ResourceId)
if ($Tagvalue.Properties.TagsProperty.Count -gt 1)
{
$Tagvalue.Id -replace "/providers/Microsoft.Resources/tags/default",""
}
}
Here is the output for reference :
Script using Azure CLI cmdlets:
$re= az resource list --tag env=prod
$rearray = $re |ConvertFrom-Json
foreach ( $re in $rearray)
{
$tagcount=$(az tag list --resource-id $re.id --query "properties.tags|length(#)")
if ($tagcount -ge 1)
{
$re.id
}
Here is the output for reference :
I'm trying to list all unmanaged disks in a specific resource group in my account inside Azure cloud provider that not have a specific tag, but having issues with the query part.
The command above lists all unmanaged disks:
az disk list -g $rgName --query [?managedBy=='null'].name -o tsv
When writing the command above, I'm not getting any output (although I have unmanaged disks that don't have tags.Action equals to 'ToDelete':
az disk list -g $rgName --query "[?(managedBy=='null') && (tags.Action!='ToDelete')].name" -o tsv
Thank you for the help :)
I believe the issue is because you are comparing against the string 'null' instead of null. This will cause you to recieve an empty array [] as the result.
This works for me:
az disk list -g $rgName --query "[?(managedBy==null) && (tags.Action!='ToDelete')].name"
How can you find the size of an existing VM using the Azure CLI? I can see how to find what sizes are available, or what sizes a VM can be resized to, but not simply what the existing size is. You'd think that might be one of the details in az vm show --show-details but it's not.
In the screenshot in my comment, I use the command.
az vm show -g '<resource group name>' -n '<VM name>' -d
You could use --query 'hardwareProfile.vmSize' to get the vmSize directly.
az vm show -g '<resource group name>' -n '<VM name>' --query 'hardwareProfile.vmSize' -o tsv
Just as additional info, when using
az vm show -g <resourcegroupname> -n <vmname>
you can see which info is available to access.
For the VMSize you would notice in the responsetree:
JSONResponse
Which you can then access by using the query posted by Joy Wang.