Q:

Import AppInsight Instrumentation Key through Release/CD and Update Instrumentation key in appsettings of an Application

I have two CD/Release Definition; one is ARM for Application Insight and another CD is for deploying the App.

First one is creating Application Insight in Azure.
Second CD is used to deploy the App.[App is not a WebApp; it's API deploying in SF Cluster]

Planning to merge the two CD/Release definition and create a One CD/Release Definition.
once Application Insight created then get the Instrumentation key and update the instrumentation key in appsettings of App.
Found one solution from Github; which will through "output" tag in ARM and  PowerShell script to get the instrumentation key once deployment finished. But it's failing -
https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-resource-manager/resource-manager-templates-outputs.md#define-and-use-output-values
Get-AzureRmResourceGroupDeployment : Run Login-AzureRmAccount to login.
At D:\a\_temp\ded99e61-d7aa-40d7-979a-dfff517ed558.ps1:7 char:1
+ Get-AzureRmResourceGroupDeployment "XXXXXX"  "XXXXXXXXX"
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Get-AzureRmResourceGroupDeployment], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzur 
eResourceGroupDeploymentCmdlet

Code wise I have two solution one is ARM for App Insight and another one is SF Cluster App Solution. I can not pull all the ARM and merged it in the App solution. 

My tasks are -

CD is failing in "PowerShell Script" task.
Please provide the best possible ways to do this. 

A:

When running a script that interacts with Azure, use the Azure PowerShell task. 
Specify a variable for the Deployment Outputs parameter in the Azure Resource Group Deployment task, and you'll get a variable populated with the outputs (which, IIRC, should contain the deployment name). It's a JSON object, so you can parse it with ConvertFrom-JSON. 
If you specify a variable named Foo, you can access it in a down-stream script as $env:Foo. 

