Merge pull request #2371 from microsoft/micmax/refactor-post-telemetry-script

Refactor PostTelemetry.ps1 script
This commit is contained in:
Michael Maxwell 2021-08-20 15:03:21 -07:00 коммит произвёл GitHub
Родитель 18e6160a1d 241e198cd3
Коммит a04d0e4b39
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 39 добавлений и 11 удалений

Просмотреть файл

@ -1,13 +1,40 @@
$url="https://{IngestionEndpoint-from-Component-ConnectionString}/v2/track"
$iKey="{test-iKey}"
Write-Host $url, $iKey
param (
[string]$ConnectionString,
[string]$InstrumentationKey
)
$time = Get-Date
$time = $time.ToUniversalTime().ToString('o')
function ParseConnectionString {
param (
[string]$ConnectionString
)
$availabilityData = @"
$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": {
"data": {
"baseData": {
"ver": 2,
"id": "TestId",
@ -22,14 +49,15 @@ $availabilityData = @"
},
"baseType": "AvailabilityData"
},
"ver": 1,
"ver": 1,
"name": "Microsoft.ApplicationInsights.Metric",
"time": "$time",
"time": "$Time",
"sampleRate": 100,
"iKey": "$iKey",
"iKey": "$IKey",
"flags": 0
}
"@
Invoke-WebRequest -Uri $url -Method POST -Body $availabilityData -Verbose -Debug -UseBasicParsing
Write-Host "URL: $Url, IKey: $IKey"
# Expected Output: {"itemsReceived":1,"itemsAccepted":1,"errors":[]}
Invoke-WebRequest -Uri $Url -Method POST -Body $AvailabilityData -Verbose -Debug -UseBasicParsing