This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.ps1
148 lines (136 loc) · 4.54 KB
/
profile.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Azure Functions profile.ps1
#
# This profile.ps1 will get executed every "cold start" of your Function App.
# "cold start" occurs when:
#
# * A Function App starts up for the very first time
# * A Function App starts up after being de-allocated due to inactivity
#
# You can define helper functions, run commands, or specify environment variables
# NOTE: any variables defined that are not environment variables will get reset after the first execution
# Authenticate with Azure PowerShell using MSI.
# Remove this if you are not planning on using MSI or Azure PowerShell.
if ($env:MSI_SECRET -and (Get-Module -ListAvailable Az.Accounts)) {
Connect-AzAccount -Identity
}
$ErrorActionPreference = "Stop"
Function Get-OMSAPISignature
{
Param
(
[Parameter(Mandatory = $True)]$customerId,
[Parameter(Mandatory = $True)]$sharedKey,
[Parameter(Mandatory = $True)]$date,
[Parameter(Mandatory = $True)]$contentLength,
[Parameter(Mandatory = $True)]$method,
[Parameter(Mandatory = $True)]$contentType,
[Parameter(Mandatory = $True)]$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)
$authorization = 'SharedKey {0}:{1}' -f $customerId,$encodedHash
return $authorization
}
Function Send-OMSAPIIngestionFile
{
Param
(
[Parameter(Mandatory = $True)]$customerId,
[Parameter(Mandatory = $True)]$sharedKey,
[Parameter(Mandatory = $True)]$body,
[Parameter(Mandatory = $True)]$logType,
[Parameter(Mandatory = $False)]$TimeStampField,
[Parameter(Mandatory = $False)]$ResourceId,
[Parameter(Mandatory = $False)]$EnvironmentName
)
#<KR> - Added to encode JSON message in UTF8 form for double-byte characters
$body=[Text.Encoding]::UTF8.GetBytes($body)
$method = "POST"
$contentType = "application/json"
$resource = "/api/logs"
$rfc1123date = [DateTime]::UtcNow.ToString("r")
$contentLength = $body.Length
$signature = Get-OMSAPISignature `
-customerId $customerId `
-sharedKey $sharedKey `
-date $rfc1123date `
-contentLength $contentLength `
-method $method `
-contentType $contentType `
-resource $resource
if($EnvironmentName -eq "AzureUSGovernment")
{
$Env = ".ods.opinsights.azure.us"
}
Else
{
$Env = ".ods.opinsights.azure.com"
}
$uri = "https://" + $customerId + $Env + $resource + "?api-version=2016-04-01"
if ($TimeStampField.length -gt 0)
{
if ($ResourceId.length -gt 0)
{
$headers = @{
"Authorization" = $signature;
"Log-Type" = $logType;
"x-ms-date" = $rfc1123date;
"time-generated-field"=$TimeStampField;
"x-ms-AzureResourceId"=$ResourceId;
}
}
else
{
$headers = @{
"Authorization" = $signature;
"Log-Type" = $logType;
"x-ms-date" = $rfc1123date;
"time-generated-field"=$TimeStampField;
}
}
}
else {
if ($ResourceId.length -gt 0)
{
$headers = @{
"Authorization" = $signature;
"Log-Type" = $logType;
"x-ms-date" = $rfc1123date;
"x-ms-AzureResourceId"=$ResourceId;
}
}
else
{
$headers = @{
"Authorization" = $signature;
"Log-Type" = $logType;
"x-ms-date" = $rfc1123date;
}
}
}
$response = Invoke-WebRequest `
-Uri $uri `
-Method $method `
-ContentType $contentType `
-Headers $headers `
-Body $body `
-UseBasicParsing `
-verbose
if ($response.StatusCode -ge 200 -and $response.StatusCode -le 299)
{
write-output 'Accepted'
}
else
{
Write-Output $response.StatusCode
}
}
# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
# Enable-AzureRmAlias
# You can also define functions or aliases that can be referenced in any of your PowerShell functions.