How can I programmatically determine the name of the Application Insights instance given the instrumentation key?
Our company has a large number of application insights instances in Azure. When troubleshooting an application, it can take quite a while to track down the right app insights instance for a particular app.
I should have specified (more than just using C# tag), that I was looking for a C# solution. Ideally, I would like to embed something so I could implement a page like 'myapp.com/appinsights' and this would give me the correct app insights instance (of the hundreds that we have) for a given app.
The older AzureRM
PowerShell module is being replaced by the new cross-platform Az
module. Based on the answers of @tobias and @ranieuwe, the following can fetch all your InstrumentationKeys using the newer module.
Az
moduleInstall-Module -Name Az -AllowClobber
as admin, or
Install-Module -Name Az -AllowClobber -Scope CurrentUser
as non-admin
Full instructions here: https://docs.microsoft.com/en-us/powershell/azure/install-az-ps
If you get warnings about both Az
and AzureRM
being installed/loaded, you can uninstall the old module by running the following as admin: Uninstall-AzureRm
Import-Module Az
Connect-AzAccount
Get-AzSubscription # will list all currently connected subscriptions
Select-AzSubscription <subscription-id>
# Retrieve all Instrumentation Keys along with name of AppInsights resource
Get-AzResource -ExpandProperties -ResourceType "microsoft.insights/components" | Select -ExpandProperty Properties | Select Name, InstrumentationKey
# Find a specific Instrumentation Key
Get-AzResource -ExpandProperties -ResourceType "microsoft.insights/components" | Select -ExpandProperty Properties | Where InstrumentationKey -eq "abe66a40-c437-4af1-bfe9-4b72bd6b94a1"| Select Name, InstrumentationKey
Using azure cloud shell (or any shell where you have azure-cli ^2.0.64 installed):
az extension add --name application-insights
az monitor app-insights component show --output table | grep <instrumentation_key>
This searches across your current subscription. You can see your current subscription with
az account show
There are probably fancier ways to use --query but the above approach is general purpose.
You can do this using PowerShell with the AzureRm cmdlets. If you are new to that, take a look here at the Azure Resource Manager.
You'll first need to login with Login-AzureRmAccount
and then select a subscription with Select-AzureRmSubscription
The following script will get a list of the name of each Application Insights instance and its instrumentationkey:
Get-AzureRmResource -ExpandProperties -ResourceType "microsoft.insights/components" -ResourceGroupName "your-resource-group" | select -ExpandProperty Properties | Select Name, InstrumentationKey
This works as follows:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With