-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphBatch-Delete.ps1
189 lines (144 loc) · 5.95 KB
/
GraphBatch-Delete.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
clear-host
if ( (Test-Path -Path ".\settings.json") -eq $false )
{
">> settings.json not found!`n"
break
}
else
{
$settings = Get-Content -Path .\settings.json | ConvertFrom-Json
}
$startTime = Get-Date
$Token_Body = @{
"tenant" = $settings.tenant
"grant_type" = "client_credentials"
"client_id" = $settings.client_id
"client_secret" = $settings.client_secret
"resource" = "https://graph.microsoft.com/"
}
$Token_Params = @{
"URI" = "https://login.microsoftonline.com/$($Token_Body.tenant)/oauth2/token"
"Body" = $Token_Body
"ContentType" = "application/x-www-form-urlencoded"
"Method" = "POST"
}
$Token_GraphAPI = Invoke-RestMethod @Token_Params
$Token_ExpirationTime = (Get-Date).AddSeconds($Token_GraphAPI.expires_in)
# Site ID for $settings.SPOSite
$requestSite = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/sites/$($settings.SPORootSite):/sites/$($settings.SPOSite)" `
-Headers @{"Authorization" = "Bearer $($Token_GraphAPI.access_token)"} `
-ContentType "application/json; charset=utf-8" -Method GET
if ($null -ne $requestSite)
{
$siteID = $requestSite.id.Split(",")[1]
}
else
{
">> Site '$($settings.SPOSite)' not found!`n"
break
}
# the List ID for $settings.SPOList
$requestList = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/sites/$($siteId)/lists/$($settings.SPOList)" `
-Headers @{"Authorization" = "Bearer $($Token_GraphAPI.access_token)"} `
-ContentType "application/json; charset=utf-8" -Method GET
if ($null -ne $requestList)
{
$listID = $requestList.id
}
else
{
">> List '$($settings.SPOList)' not found!`n"
break
}
# deletion process begins from the first ID available
$requestID = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/sites/$($siteID)/lists/$($listID)/items?`$top=1" `
-ContentType "application/json" `
-Method Get -Headers @{ Authorization = "Bearer $($Token_GraphAPI.access_token)" }
if ($null -eq $requestID)
{
"- There are no more items!`n"
exit
}
# where to start and finish (SPO List item IDs)
$itemDeleteStart = $requestID.value.id
$itemDeleteEnd = 5000
# self-contained, useful variables for the deletion loop
$batchTotal = [math]::ceiling( ($itemDeleteEnd - $itemDeleteStart) / 20 )
$leadingZeroes = "d" + $($batchTotal).ToString().Length
$batchCurrent = $itemsQueued = $index = $dependsOn = 0
$showOutput = $false
$payload = $requests = $request = @()
#
# Performs the deletion of items from "itemDeleteStart" to "itemDeleteEnd"
#
$itemDeleteStart..$itemDeleteEnd | % {
$index++
$itemsQueued++
$dependsOn++
$request = @{
id = $_
url = "/sites/$siteId/lists/$listId/items/$_"
headers = @{ "Content-Type" = "application/json" }
method = "DELETE"
}
if ($dependsOn -gt 1)
{
$request.Add( "dependsOn", @($_-1) )
}
$requests += $request
if ($itemsQueued -eq 20)
{
$batchCurrent++
$showOutput = $true
$dependsOn = 0
$payload = @{ requests = $requests } | ConvertTo-Json -Depth 4
#
# Renews the token when it expires
#
if ((Get-Date) -ge $Token_ExpirationTime)
{
"`n`t »» Issuing new token ... "
$Token_GraphAPI = Invoke-RestMethod @Token_Params
$Token_ExpirationTime = (Get-Date).AddSeconds($Token_GraphAPI.expires_in)
"`n`t »» New token issued!"
}
$batchRequest = $null
$timeRequest = Measure-Command {
$batchRequest = Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/$batch' `
-ContentType "application/json" `
-Body $payload -Method Post `
-Headers @{ Authorization = "Bearer $($Token_GraphAPI.access_token)" }
if ($batchRequest -EQ $null)
{
#
# TO-DO: needs some better handling for failed requests - for now just aborting the process
#
"`n -- Error - Aborting process`n"
break
}
}
$itemsQueued = 0
$requests = @()
}
if ($timeRequest -ne $null)
{
$timeTotal += $timeRequest
}
if ($showOutput)
{
if ($timeRetry -eq $null)
{
"════ Batch {0:$($leadingZeroes)} of {1} `t`t`t request: {2:d1}m:{3:d2}s.{4:d3}ms `t`t time: {5:d2}h:{6:d2}m:{7:d2}s.{8:d3}ms `n" -f $batchCurrent, $batchTotal, $timeRequest.Minutes, $timeRequest.Seconds, $timeRequest.Milliseconds, $timeTotal.Hours, $timeTotal.Minutes, $timeTotal.Seconds, $timeTotal.Milliseconds
}
else
{
$timeTotal.Add($timeRetry)
"════ Batch {0:$($leadingZeroes)} of {1} `t`t`t request: {2:d1}m:{3:d2}s.{4:d3}ms `t`t`t retry: {5:d1}m:{6:d2}s.{7:d3}ms `t`t time: {8:d2}h:{9:d2}m:{9:d2}s.{10:d3}ms `n" -f $batchCurrent, $batchTotal, $timeRequest.Minutes, $timeRequest.Seconds, $timeRequest.Milliseconds, $timeRetry.Minutes, $timeRetry.Seconds, $timeRetry.Milliseconds, $timeTotal.Hours, $timeTotal.Minutes, $timeTotal.Seconds, $timeTotal.Milliseconds
}
$timeRequest = $null
$showOutput = $false
}
}
$endTime = Get-Date
$totalTime = $endTime - $startTime
"`n`n════════════════»» Total runtime: {0:d2}h:{1:d2}min:{2:d2}s.{3:d3}ms" -f $totalTime.Hours, $totalTime.Minutes, $totalTime.Seconds, $totalTime.Milliseconds