Last active
July 30, 2021 16:23
-
-
Save dylanberry/a7d800fbe396b2eddeb4f71d06b5f32b to your computer and use it in GitHub Desktop.
Revisions
-
dylanberry revised this gist
Jul 30, 2021 . 1 changed file with 15 additions and 17 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -36,17 +36,15 @@ Function Build-AuthorizationSignature ($WorkspaceId, $SharedKey, $Date, $Content 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 ` @@ -58,13 +56,13 @@ Function Post-LogData($WorkspaceId, $SharedKey, $TimeStampField, $LogType, $Data -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 @@ -75,16 +73,16 @@ az login --allow-no-subscriptions --service-principal --username $ClientId --pas $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 -
dylanberry created this gist
Jul 30, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,90 @@ 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, $LogType, $Data) { $Data $bodyAsJson = ConvertTo-Json $Data $bodyAsJson $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://$CustomerId.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 $environmentCapacity = 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)"} $environment = $environmentCapacity.value.properties | ? displayName -eq $EnvironmentName $logAnalyticsParams = @{ WorkspaceId = $WorkspaceId SharedKey = $SharedKey TimeStampField = "CollectionTime" LogType = "Process" } Post-LogData @logAnalyticsParams -Data $environment.capacity