-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathPostTelemetry.ps1
63 lines (55 loc) · 1.58 KB
/
PostTelemetry.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
param (
[string]$ConnectionString,
[string]$InstrumentationKey
)
function ParseConnectionString {
param (
[string]$ConnectionString
)
$Map = @{}
foreach ($Part in $ConnectionString.Split(";")) {
$KeyValue = $Part.Split("=")
$Map.Add($KeyValue[0], $KeyValue[1])
}
return $Map
}
# Exit with error if either both or neither of these parameters are provided
if (("" -eq $ConnectionString) -eq ("" -eq $InstrumentationKey)) {
Write-Error "Please provide one of the parameters: 'ConnectionString' or 'InstrumentationKey'" -ErrorAction Stop
}
# Build the connection string using the instrumentation key
If ($InstrumentationKey) {
$ConnectionString = "InstrumentationKey=$InstrumentationKey;IngestionEndpoint=https://dc.services.visualstudio.com/"
}
$Map = ParseConnectionString($ConnectionString)
$Url = $Map["IngestionEndpoint"] + "v2/track"
$IKey = $Map["InstrumentationKey"]
$Time = (Get-Date).ToUniversalTime().ToString("o")
$AvailabilityData = @"
{
"data": {
"baseData": {
"ver": 2,
"id": "TestId",
"name": "Post Telemetry Test",
"duration": "10.00:00:00",
"success": true,
"runLocation": "TestLocation",
"message": "Test Message",
"properties": {
"TestProperty": "TestValue"
}
},
"baseType": "AvailabilityData"
},
"ver": 1,
"name": "Microsoft.ApplicationInsights.Metric",
"time": "$Time",
"sampleRate": 100,
"iKey": "$IKey",
"flags": 0
}
"@
Write-Host "URL: $Url, IKey: $IKey"
# Expected Output: {"itemsReceived":1,"itemsAccepted":1,"errors":[]}
Invoke-WebRequest -Uri $Url -Method POST -Body $AvailabilityData -Verbose -Debug -UseBasicParsing