Private/Export-ObjectToLogAnalytics.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 64 65 66 67 68 69 70 71 |
Function Export-ObjectToLogAnalytics { [cmdletbinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] Param( [Parameter(Mandatory = $true, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] [string] $ALWorkspaceID, [Parameter(Mandatory = $true, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] [string] $WorkspacePrimaryKey, [Parameter(Mandatory = $true, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] [ValidateNotNullOrEmpty()] [psobject[]] $PSObject, [Parameter(Mandatory = $true, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] [ValidateNotNullOrEmpty()] $ALTableIdentifier, [Parameter(Mandatory = $true, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] [ValidateNotNullOrEmpty()] $TimeStampField ) process { $bodyAsJson = ConvertTo-Json $PSObject $body = [System.Text.Encoding]::UTF8.GetBytes($bodyAsJson) $method = 'POST' $resource = '/api/logs' $rfc1123date = [DateTime]::UtcNow.ToString("r") $contentType = 'application/json' $getLogAnalyticsSignatureSplat = @{ ALWorkspaceID = $ALWorkspaceID WorkspacePrimaryKey = $WorkspacePrimaryKey Date = $rfc1123date ContentLength = $body.Length Method = $method ContentType = $contentType Resource = $resource } $signature = Get-LogAnalyticsSignature @getLogAnalyticsSignatureSplat $uri = "https://{0}.ods.opinsights.azure.com{1}?api-version=2016-04-01" -f $ALWorkspaceID, $resource $headers = @{ "Authorization" = $signature; "Log-Type" = $ALTableIdentifier; "x-ms-date" = $rfc1123date; "time-generated-field" = $TimeStampField; } $invokeWebRequestSplat = @{ ContentType = $contentType Method = $method UseBasicParsing = $true Uri = $uri Headers = $headers Body = $body } $response = Invoke-WebRequest @invokeWebRequestSplat $response.StatusCode } } |