-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathApplicationInsightsPurgeTelemetry.ps1
More file actions
63 lines (52 loc) · 2.32 KB
/
ApplicationInsightsPurgeTelemetry.ps1
File metadata and controls
63 lines (52 loc) · 2.32 KB
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
#### AUTHENTICATION ####
# Import the ADAL module found in AzureRM.Profile
Import-Module AzureRM.Profile
#Application ID of the registered app
$appId = "YOUR_APPLICATION_ID"
#Client secret value for the app registration
$key = "YOUR_CLIENT_SECRET"
#AAD tenant ID
$tenantId = "YOUR_AAD_TENANT_ID"
#Azure Subscription ID
$subscriptionId = "YOUR_SUBSCRIPTION_ID"
#Resource group where Application Insight Instance is assigned to
$resourceGroupName = "YOUR_APPLICATION_INSIGHTS_RESOURCE_GROUP"
#Name of the Application Insight instance
$resourceName = "YOUR_APPLICATION_INSIGHTS_INSTANCE_NAME"
# Create the authentication URL and get the authentication context
$authUrl = "https://login.windows.net/${tenantId}"
$AuthContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authUrl
# Build the credential object and get the token form AAD
$credentials = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential -ArgumentList $appId,$key
$result = $AuthContext.AcquireToken("https://management.core.windows.net/",$credentials)
# Build the authorization header JSON object
$authHeader = @{
'Content-Type'='application/json'
'Authorization'=$result.CreateAuthorizationHeader()
}
#### END AUTHENTICATION ####
#### PURGE DATA ####
#Creates the API URI
$URI = "https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Insights/components/${resourceName}/purge?api-version=2015-05-01"
$body = @"
{
"table": "customEvents",
"filters": [
{
"column": "timestamp",
"operator": "<",
"value": "2018-01-01T00:00:00.000"
}
]
}
"@
#Invoke the REST API to purge the data on Application Insights
$purgeID=Invoke-RestMethod -Uri $URI -Method POST -Headers $authHeader -Body $body
# Write the purge ID
Write-Host $purgeID.operationId -ForegroundColor Green
#### END PURGE DATA ####
#### GET PURGE STATUS ####
# Creation of the API URI to get the purge status
$purgeURI="https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/${resourceGroupName}/providers/Microsoft.Insights/components/${resourceName}/operations/$($purgeID.operationId)?api-version=2015-05-01"
Invoke-RestMethod -Uri $purgeURI -Method GET -Headers $authHeader
#### END GET PURGE STATUS ####