Export list of app insight names and InstrumentationKey - azure

I need to export a list of Application Insights with two columns: Name and Instrumentation Key.
With the following command I get the names, but the column InstrumentationKey is always null.
az resource list --resource-type "Microsoft.Insights/components" --query "[].{Name:name, InstrumentationKey:properties.InstrumentationKey}"
Without filtering the columns, by running simply
az resource list --resource-type "Microsoft.Insights/components"
I can't find the Instrumentation Key.
What is the command to get the Instrumentation Key and relative name for all the resources in a subscription?

After reproducing from my end, I could able to get the expected results using az monitor app-insights component show. Below is complete Query that worked for me.
$a = az monitor app-insights component show | ConvertFrom-Json
$a | Select-Object name,instrumentationKey
RESULTS:

Related

How can i query Azure for resources using Powershell with multiple query options?

I am query for resources in a subscription as follows:
(az resource list --subscription <subscription-id> --query "[?
type=='Microsoft.Web/sites'] "| ConvertFrom-Json)
But i want to put a second query parameter like the following
(az resource list --subscription <subscription-id> --query "[?
type=='Microsoft.Web/sites'&& resourcegroup== $resourcegroup.name ]"|
ConvertFrom-Json)
How can i do this ?
For querying multiple parameters you can use az graph query in azure cli as below and I followed Microsoft-Document:
Firstly, Set account subscription as below:
az account set --name "XX"
XX- Subscription name.
Now query to get resources:
az graph query -q "Resources | where type=~ 'microsoft.web/sites' | project resourceGroup, name"| ConvertFrom-Json
Please try by using the Pipe expression (|) to separate each condition. Your command would be something like:
(az resource list --subscription <subscription-id> --query "[?
type=='Microsoft.Web/sites'] | [?resourceGroup=='$resourcegroup.name']" |
ConvertFrom-Json)
Please ensure that you are using proper casing for property names (e.g. resourceGroup instead of resourcegroup) and string values are inside single quotes.
Here's the command I used to get storage accounts in a resource group in my Azure Subscription:
az resource list --query "[? type=='Microsoft.Storage/storageAccounts'] | [?resourceGroup=='my-resource-group-name']"

How to see app insights of a particular subscription through powershell?

I want to get app insights of all the subscriptions available in the portal but whenever I run the script
$resources = az monitor app-insights component show | ConvertFrom-Json
I get app insights only for the same subscription every time , even during the time when I change the subscription through the script
Set-AzContext -SubscriptionName "some-name"
the whole script goes like this
Set-AzContext -SubscriptionName "some-name"
$resources = az monitor app-insights component show | ConvertFrom-Json
So even if I change the subscription name to something else suppose "some-name1"
still I am getting the app-insights for subscription "some-name"
This is by design.
While you could switch the context in a script, searches across multiple subscriptions are easier and much, much faster using the Resource Graph.
PowerShell Query:
Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
Azure CLI Query:
az graph query -q "resources | where type =~ 'Microsoft.Insights/components'"
Both options should get you all Application Insights resources across your tenant.
For more details, please see the Starter Resource Graph query samples.
On a side note I would also recommend to stick to either Azure CLI or Az PowerShell. While the choice of language is personal preference, sticking to one of the two decreases the dependencies. If you stick to Azure CLI, the only prerequisite is having the Azure CLI binaries installed. If you stick to Az Modules in PowerShell, you don't need Azure CLI but only the Az Modules. Mixing both makes the code more difficult to port to other machines.
So, if using the Az Modules was preferred, instead of...
$resources = az monitor app-insights component show | ConvertFrom-Json
I would recommend:
$resources = Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
The issue you're experiencing with the Set-AzContext command is that it only sets the subscription context for the current PowerShell session.
The az monitor app-insights component show command is running in a separate process or thread, so it is not able to see the updated subscription context set by the Set-AzContext command.
To work around this, you can pass the -Subscription parameter to the az monitor app-insights component show command, like so:
$resources = az monitor app-insights component show --subscription "some-name1" | ConvertFrom-Json
This will ensure that the az command is running with the correct subscription context, and you will get the app insights for the correct subscription.

az vmss list command returns empty value

I have two resource groups in Azure, each contains one VMSS (Virtual Machine Scale Sets) and the resources are visible in Azure Portal. The problem is, the following commands return empty output:
az resource list --subscription MySubscription -g vmss-rg
az vmss list --subscription MySubscription -g vmss-rg
az vmss list
However, running az resource list with different resource groups which doesn't have VMSS works fine. I also tested it with different Azure account and subscription, it also worked.
We have tested the same command in our local as suggested by #VenkateshDodda-MT and it works fine. Posting it as an answer to help other community members for the similar issue so that they can find and fix their problem.
To achieve the above requirement we need to install the az powershell module in our local .
Post that run the command in powershell to get the vmss list under our subscription or resource groups:-
az resource list --resource-type "Microsoft.Compute/virtualMachineScaleSets"
OUTPUT:-
To get the VMSS list under particular resource group run the below cmd:
az resource list --resource-type "Microsoft.Compute/virtualMachineScaleSets" -g '<rgName>'
OUTPUT:-
For more information please refer this SO THREAD:Azure PS command returns empty list as suggested by #Olga Osinskaya - MSFT

AZ command to fetch Azure Monitor logs

I have to fetch logs using AZ commands:
If I add a filter on Resource Group, it is not giving result.
For e.g.--
Following commands is working fine:
az monitor activity-log list --subscription "subscription1"
In the result I am getting logs for ResourceGroup1.
But when I execute following:
az monitor activity-log list --resource-group "ResourceGroup1"
It is giving 0 result.
I am using "azure-cli": "2.26.1"
You can use the below command to read monitor activity logs at resource group level
az monitor activity-log list -g "resource-group"
Here is the command to pull activity monitor logs with filters
az monitor activity-log list -g 'resource-group' --start-time 2021-07-29T12:00:00 --select {ResourceGroupName,EventTimestamp,CorrelationId,ResourceId} -o table
When we do AZ login, there is one field "isDefault": true for only one subscription (Default subscription of your account). For other Subscriptions, it will be False.
You will get results for all RGs under default Subscription. For other RGs, it will give 0 result.
Therefore, when you apply filter on a RG, you need to confirm that it's corresponding Subscription should be set as default. You can set same as:
az account set -s <subscription ID>

How to find resource groups of particular resource in az cli

I have resource name , but I have to find resource group to which resource belongs. It needs to be done using az cli
You can use the command below to list resources by resource name :
az resource list -n '<resource name>'
So that you can find resourceGroup property in result:
In addition to other answers provided, if you wanted to filter the results of az resource list to show just the resource group name, you could query the json output for the resourceGroup key and then output as tab-separated values.
az resource list -n resource-name --query "[].resourceGroup" -o tsv
Which will just output like this:
ked#mac:~/repos|master ⇒ az resource list -n my-resource --query "[].resourceGroup" -o tsv
my-rg
Here's the azure documentation that shows how to format output.

Resources