简体   繁体   中英

Link application insights to Azure web app through powershell

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM