Last active
July 30, 2021 16:23
-
-
Save dylanberry/a7d800fbe396b2eddeb4f71d06b5f32b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)] | |
[string]$ClientId, | |
[Parameter(Mandatory=$true)] | |
[string]$ClientSecret, | |
[Parameter(Mandatory=$true)] | |
[string]$TenantId, | |
[Parameter(Mandatory=$true)] | |
[string]$EnvironmentName, | |
[Parameter(Mandatory=$true)] | |
[string]$WorkspaceId, | |
[Parameter(Mandatory=$true)] | |
[string]$SharedKey | |
) | |
# https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-collector-api | |
Function Build-AuthorizationSignature ($WorkspaceId, $SharedKey, $Date, $ContentLength, $Method, $ContentType, $Resource) | |
{ | |
$xHeaders = "x-ms-date:$date" | |
$stringToHash = "$Method`n$ContentLength`n$ContentType`n$xHeaders`n$Resource" | |
$bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash) | |
$keyBytes = [Convert]::FromBase64String($SharedKey) | |
$sha256 = New-Object System.Security.Cryptography.HMACSHA256 | |
$sha256.Key = $keyBytes | |
$calculatedHash = $sha256.ComputeHash($bytesToHash) | |
$encodedHash = [Convert]::ToBase64String($calculatedHash) | |
return "SharedKey $($WorkspaceId):$($encodedHash)" | |
} | |
Function Post-LogData($WorkspaceId, $SharedKey, $TimeStampField, $ResourceId, $LogType, $Data) | |
{ | |
$bodyAsJson = ConvertTo-Json $Data | |
$body = [System.Text.Encoding]::UTF8.GetBytes($bodyAsJson) | |
$method = 'POST' | |
$contentType = 'application/json' | |
$resource = '/api/logs' | |
$rfc1123date = [DateTime]::UtcNow.ToString('r') | |
$authorizationSignature = Build-AuthorizationSignature ` | |
-WorkspaceId $WorkspaceId ` | |
-SharedKey $SharedKey ` | |
-Date $rfc1123date ` | |
-ContentLength $body.Length ` | |
-Method $method ` | |
-ContentType $contentType ` | |
-Resource $resource | |
$headers = @{ | |
'Authorization' = $authorizationSignature; | |
'Log-Type' = $LogType; | |
'x-ms-date' = $rfc1123date; | |
'time-generated-field' = $TimeStampField; | |
} | |
$uri = "https://$WorkspaceId.ods.opinsights.azure.com$($resource)?api-version=2016-04-01" | |
$response = Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Headers $headers -Body $body -UseBasicParsing | |
return $response.StatusCode | |
} | |
az login --allow-no-subscriptions --service-principal --username $ClientId --password $ClientSecret --tenant $TenantId | |
$resource = 'https://api.bap.microsoft.com/' | |
$token = az account get-access-token --resource $resource | ConvertFrom-Json | |
$environmentCapacityList = Invoke-RestMethod -Uri 'https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments?api-version=2020-10-01&$expand=properties.capacity,properties.addons' ` | |
-Method GET ` | |
-Headers @{'Authorization' = "Bearer $($token.accessToken)"} | |
$environmentCapacity = $environmentCapacityList.value.properties | ? displayName -eq $EnvironmentName | select -Expand capacity | select *,@{Name='environmentName'; Expression={$EnvironmentName}} | |
$logAnalyticsParams = @{ | |
WorkspaceId = $WorkspaceId | |
SharedKey = $SharedKey | |
LogType = 'Capacity' | |
TimeStampField = $environmentCapacity[0].updatedOn | |
} | |
Post-LogData @logAnalyticsParams -Data $environmentCapacity |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment