forked from demiliani/PowershellCloudScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateApplicationInsightsD365BCTelemetryConfiguration.ps1
68 lines (52 loc) · 3.08 KB
/
CreateApplicationInsightsD365BCTelemetryConfiguration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Name of the Application Insights Resource
$appInsightsName = "YOURAPPLICATIONINSIGHTSNAME"
#Name of the Resource Group to use.
$resourceGroupName = "YOURRESOURCEGROUPNAME"
#Name of the workspave
$WorkspaceName = "YOURWORKSPACENAME"
#Azure location
$Location = "westeurope"
#Data retention for Application Insights (days)
$dataretentiondays = 30
#Daily Cap (GB) for Application Insights instance
$dailycap = 15
#Parameters for connecting to Dynamics 365 Business Central tenant
#Business Central tenant id
$aadTenantId = "TENANTID"
#Name of the D365BC Environment
$D365BCenvironmentName = "YOURD365BCENVIRONMENTNAME"
#Partner's AAD app id
$aadAppId = "CLIENTID"
#Partner's AAD app redirect URI
$aadAppRedirectUri = "nativeBusinessCentralClient://auth"
Connect-AzAccount
New-AzResourceGroup -Name $resourceGroupName -Location $Location
New-AzOperationalInsightsWorkspace -Location $Location -Name $WorkspaceName -ResourceGroupName $resourceGroupName
$Resource = Get-AzOperationalInsightsWorkspace -Name $WorkspaceName -ResourceGroupName $resourceGroupName
$workspaceId = $Resource.ResourceId
New-AzApplicationInsights -ResourceGroupName $resourceGroupName -Name $appInsightsName -location $Location -WorkspaceResourceId $workspaceId
$Resource = Get-AzResource -ResourceType Microsoft.Insights/components -ResourceGroupName $resourceGroupName -ResourceName $appInsightsName
$connectionString = $resource.Properties.ConnectionString
Write-Host "Connection String = " $connectionString
#Set data retention
$Resource.Properties.RetentionInDays = $dataretentiondays
$Resource | Set-AzResource -Force
#Set daily cap (GB)
Set-AzApplicationInsightsDailyCap -ResourceGroupName $resourceGroupName -Name $appInsightsName -DailyCapGB $dailycap
# Load Microsoft.IdentityModel.Clients.ActiveDirectory.dll
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.140\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" # Install-Module AzureAD to get this
# Get access token
$ctx = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new("https://login.microsoftonline.com/$aadTenantId")
$redirectUri = New-Object -TypeName System.Uri -ArgumentList $aadAppRedirectUri
$platformParameters = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList ([Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always)
$accessToken = $ctx.AcquireTokenAsync("https://api.businesscentral.dynamics.com", $aadAppId, $redirectUri, $platformParameters).GetAwaiter().GetResult().AccessToken
Write-Host $accessToken
$response = Invoke-WebRequest `
-Method Post `
-Uri "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$D365BCenvironmentName/settings/appinsightskey" `
-Body (@{
key = $connectionString
} | ConvertTo-Json) `
-Headers @{Authorization=("Bearer $accessToken")} `
-ContentType "application/json"
Write-Host "Responded with: $($response.StatusCode) $($response.StatusDescription)"