0
votes

I'm trying to link new application insights to existing Azure web app through Powershell with the below script. I'm able to create a new app insight but unable to link the new app insight to the existing Azure web app.

$appInsights = New-AzResource -ResourceName 'MyWebsite09' -ResourceGroupName 'Test' `
-Tag @{ applicationType = 'web'; applicationName = 'sample1'} `
-ResourceType 'Microsoft.Insights/components' -Location 'North Central US' `
-PropertyObject @{'Application_Type'='web'} -Force

$appSetting = @{'APPINSIGHTS_INSTRUMENTATIONKEY'= $appInsights.Properties.InstrumentationKey}
Set-AzWebApp -Name 'sample1' -ResourceGroupName 'Test' -AppSettings $appSetting
2
The commands seem correct. What error did you face? - Pamela Peng - MSFT
I'm not facing any error. This command is executing successfully in powershell which is creating new app insight in the RG but it is unable to link the new app insight to existing webapp. - kiran

2 Answers

0
votes

Here is the Powershell commands to link application insights with exisiting azure web app . As your code will not enable the application insights , Follow the below code

$app = Get-AzWebApp -ResourceGroupName "AppMonitoredRG" -Name "AppMonitoredSite" -ErrorAction Stop
$newAppSettings = @{} # case-insensitive hash map
$app.SiteConfig.AppSettings | %{$newAppSettings[$_.Name] = $_.Value} # preserve non Application Insights application settings.
$newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = "012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights instrumentation key
$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights connection string
$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2"; # enable the ApplicationInsightsAgent
$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop  

You can Refer to this MS DOC for linking application insights to azure web-app fully.

or, You can even refer the SO thread for more details .

0
votes

The below commands work for me in Azure portal CloudShell. After implementing the below code we were able to link New Application insight for existing webapp.

Code:

$resourceGroupName = "****"

$resourceName = "***"

$appInsightsInstrumentationKey = "***"

$app = Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $resourceName -ErrorAction Stop

$newAppSettings = @{} # case-insensitive hash map

$app.SiteConfig.AppSettings | %{$newAppSettings[$.Name] = $.Value} # preserve non Application Insights application settings.

$newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = $appInsightsInstrumentationKey; # set the Application Insights instrumentation key

$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=$appInsightsInstrumentationKey"; # set the Application Insights connection string

$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2"; # enable the ApplicationInsightsAgent

$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop

Restart-AzWebApp -ResourceGroupName "*** " -Name "***"

Please update the values and try this code in Azure Portal CloudShell.

Note: The code is not working for me in Windows Powershell ISE application.