-
Notifications
You must be signed in to change notification settings - Fork 6
/
push_physical_badging_records.ps1
163 lines (140 loc) · 4.79 KB
/
push_physical_badging_records.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Usage: .\push_physical_badging_records.ps1 -tenantId "<Tenant Id>" -appId "<AAD App Id>" -appSecret "<AAD App Secret>" -jobId "<Job Id>" -jsonFilePath "<JSON_FILE_PATH>"
param
(
[Parameter(mandatory = $true)]
[string] $tenantId,
[Parameter(mandatory = $true)]
[string] $appId,
[Parameter(mandatory = $true)]
[string] $appSecret,
[Parameter(mandatory = $true)]
[string] $jobId,
[Parameter(mandatory = $true)]
[string] $jsonFilePath,
[Parameter(mandatory = $false)]
[Int] $retryTimeout = 60
)
# Access Token Config
$oAuthTokenEndpoint = "https://login.windows.net/$tenantId/oauth2/token"
$resource = 'https://microsoft.onmicrosoft.com/4e476d41-2395-42be-89ff-34cb9186a1ac'
# End point config
$eventApiUrl = "https://webhook.ingestion.office.com"
$eventApiEndpoint = "api/signals/physicalbadging"
$chunkSize = 50000
function GetAccessToken () {
Write-Host -fore green "******Getting Access Token******"
# Token Authorization URI
$uri = "$($oAuthTokenEndpoint)?api-version=1.0"
# Access Token Body
$formData =
@{
client_id = $appId;
client_secret = $appSecret;
grant_type = 'client_credentials';
resource = $resource;
tenant_id = $tenantId;
}
# Parameters for Access Token call
$params =
@{
URI = $uri
Method = 'Post'
ContentType = 'application/x-www-form-urlencoded'
Body = $formData
}
$response = Invoke-RestMethod @params -ErrorAction Stop
Write-Host -fore green "******Access Token Acquired******"
return $response.access_token
}
function RetryCommand {
[CmdletBinding()]
Param(
[Parameter(Position = 0, Mandatory = $true)]
[scriptblock]$ScriptBlock,
[Parameter(Position = 1, Mandatory = $false)]
[int]$Maximum = 5
)
Begin {
$cnt = 0
}
Process {
do {
$cnt++
try {
$ScriptBlock.Invoke()
return
}
catch {
Write-Error $_.Exception.InnerException.Message -ErrorAction Continue
Write-Host ("Will retry in [{0}] seconds" -f $retryTimeout)
Start-Sleep $retryTimeout
if ($cnt -lt $Maximum) {
Write-Host "Retrying"
}
}
} while ($cnt -lt $Maximum)
throw 'Execution failed.'
}
}
function WriteErrorMessage($errorMessage) {
$Exception = [Exception]::new($errorMessage)
$ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
$Exception,
"errorID",
[System.Management.Automation.ErrorCategory]::NotSpecified,
$TargetObject
)
$PSCmdlet.WriteError($ErrorRecord)
}
function PushPhysicalBadgingRecords ($access_token) {
$nvCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
$nvCollection.Add('jobid', $jobId)
$uriRequest = [System.UriBuilder]"$eventApiUrl/$eventApiEndpoint"
$uriRequest.Query = $nvCollection.ToString()
$url = $uriRequest.Uri.OriginalString
try {
$allRecords = Get-Content $jsonFilePath | ConvertFrom-Json
}
catch {
WriteErrorMessage("Error reading from file. Please check if path is correct. File is not being used elsewhere and file is a valid json data")
return
}
for ($i = 0; $i -lt $allRecords.count; $i += $chunksize) {
$Chunks += , @($allRecords[$i..($i + $chunksize - 1)]);
}
$headers = @{
"Accept" = "application/json"
"Authorization" = "Bearer $access_token"
"Content-Type" = "application/json"
}
$chunkCount = 0
foreach ($chnk in $Chunks) {
$chunkCount = $chunkCount + 1
Write-Host -fore yellow "Processing chunk $chunkCount of $($Chunks.Count) with $($chnk.Length) records"
$jsonChunk = $chnk | ConvertTo-Json
try {
$result = Invoke-WebRequest -Uri $url -Method POST -Body $jsonChunk -Headers $headers -TimeoutSec 400
}
catch {
WriteErrorMessage($_)
return
}
$status_code = [int]$result.StatusCode
if ($status_code -eq 200 -or $status_code -eq 201 -or $status_code -eq 207) {
Write-Host -fore green "******Upload Successful******"
Write-Host $result.Content
}
elseif ($status_code -eq 0 ) {
throw "Service unavailable."
}
else {
$errorstring = "Failure with StatusCode [{0}] and ReasonPhrase [{1}]" -f $result.StatusCode, $result.ReasonPhrase
WriteErrorMessage($errorstring)
throw $errorstring
}
}
}
RetryCommand -ScriptBlock {
$access_token = GetAccessToken
PushPhysicalBadgingRecords($access_token)
}