forked from ThomasMarcussen/assortedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownload-OD4BAccount.ps1
260 lines (207 loc) · 8.32 KB
/
Download-OD4BAccount.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
###################################################################################################################
# Name: Download-OD4BAccount.ps1
# Author: Thomas Marcussen, [email protected]
# Date: January,2023
###################################################################################################################
<#
.Example
.\Download-OD4BAccount.ps1 -Username [email protected] -Destination "D:\OD4B" -ThreadCount 3 -Verbose
Script prerequisites:
1. Microsoft Graph PowerShell Module installed on local machine. The script automatically checks for and installs module if needed.
2. An Azure AD user that has an admin consent to approve the following permissions in Microsoft Graph application in Azure AD apps:
Organization.Read.All, User.Read.All, Directory.Read.All
#>
param ([Parameter(Mandatory)][string] $Username,
[Parameter(Mandatory)][string] $Destination,
[Parameter(Mandatory)][int] $ThreadCount
)
function ExpandOD4BFolder {
Param([parameter(Mandatory = $true)] $Folder,
[parameter(Mandatory = $true)] $FilePath
)
write-host Retrieved $Folder.name folder located under $Folder.weburl -ForegroundColor green
$filepath = ($filepath + '/' + $folder.name)
write-host $filePath -ForegroundColor yellow
$MGUserDriveItemChild = Get-MgUserDriveItemChild -UserId $Username -DriveId $MGUserDriveID -DriveItemId $Folder.Id
ForEach ($item in $MGUserDriveItemChild) {
if ($item.folder.childcount -ne $null) {
write-host $item.name is a folder
writeFoldersReport -Folder $item
$filepath = ""
ExpandOD4BFolder -Folder $item -FilePath $filepath
}
else {
writeFilesReport -file $item -filepath $filepath
write-host $item.name is a file
}
}
}
function writeFilesReport {
Param(
[parameter(Mandatory = $true)]
$File,
[parameter(Mandatory = $true)]
$filepath
)
$filepathOnDisk = $File.parentReference.path -replace '/drives/' -replace $MGUserDriveID -replace '/root:' -replace '/','\'
$object = [PSCustomObject]@{
FileName = $File.name
path = $filepathOnDisk
DriveItemID = $File.id
Job = $Job
}
$object | export-csv $Username'_OD4B_Files_Report.csv' -NoClobber -NoTypeInformation -Append
}
function writeFoldersReport {
Param(
[parameter(Mandatory = $true)]
$Folder
)
$folderpath = $Folder.parentReference.path -replace '/drives/' -replace $MGUserDriveID -replace '/root:' -replace '/','\'
$object = [PSCustomObject]@{
name = $Folder.name
webUrl = $Folder.webUrl
path = $Folder.parentReference.path
folderPath = $folderpath
}
$object | export-csv $Username'_OD4B_Folders_Report.csv' -NoClobber -NoTypeInformation -Append
}
function createFolders {
Param(
[parameter(Mandatory = $true)]
$OD4BFoldersReport,
[parameter(Mandatory = $true)]
$Destination
)
$Folders = Import-Csv $OD4BFoldersReport
foreach ($folderEntry in $Folders) {
$folderName = $folderEntry.Name
$folderPath = $folderEntry.folderpath
if (![string]::IsNullOrWhiteSpace($folderPath)) {
New-Item -Path $Destination\$folderPath -ItemType Directory -ErrorAction SilentlyContinue
Write-Host "SubFolder $Destination\$folderPath created" -f Yellow
}
else {
New-Item -Path $Destination\$folderName -ItemType Directory -ErrorAction SilentlyContinue
Write-Host "Root Folder $Destination\$folderName created" -f Yellow
}
}
}
function createBatches {
Param(
[parameter(Mandatory = $true)]
$OD4BFilesReport,
[parameter(Mandatory = $true)]
$ThreadCount
)
Get-ChildItem -Name batch* | Remove-Item -Force
$Files = Import-Csv $OD4BFilesReport
$OutputFilenamePattern = 'batch_'
$LineLimit = [math]::Floor($Files.count / $ThreadCount)
$line = 0
$i = 0
$file = 0
$start = 0
while ($line -le $Files.Length) {
if ($i -eq $LineLimit -Or $line -eq $Files.Length) {
$file++
$Filename = "$OutputFilenamePattern$file"
$Files[$start..($line - 1)] | export-csv $Filename'_'$Username'_OD4B_Files_Report.csv' -NoTypeInformation -Force
$start = $line;
$i = 0
Write-Host "Created batch file $Filename"
}
$i++;
$line++
}
}
function downloadFiles {
Param(
[parameter(Mandatory = $true)]
$Username,
[parameter(Mandatory = $true)]
$MGUserDriveID,
[parameter(Mandatory = $true)]
$Destination,
[parameter(Mandatory = $true)]
$ThreadCount
)
$ScriptBlock = {
param(
$list = $null,
$Destination = "D:\OD4B",
$Username = $null,
$MGUserDriveID = $null
)
foreach($fileEntry in $list) {
$FilePath = $fileEntry.path
$DriveItemID = $fileEntry.DriveItemID
Get-MgUserDriveItemContent -DriveId $MGUserDriveID -UserId $Username -DriveItemId $DriveItemID -outfile "$Destination$FilePath"
}
}
$MaxRunspaces = $ThreadCount+1
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxRunspaces)
$RunspacePool.Open()
$Jobs = New-Object System.Collections.ArrayList
$Filenames = Get-ChildItem -Name batch*
foreach ($File in $Filenames) {
$batch = Import-Csv $File
Write-Host "Creating runspace for $File"
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
$PowerShell.AddScript($ScriptBlock).AddParameter("list",$batch).AddParameter("Destination",$Destination).AddParameter("Username",$Username).AddParameter("MGUserDriveID",$MGUserDriveID) | Out-Null
$JobObj = New-Object -TypeName PSObject -Property @{
Runspace = $PowerShell.BeginInvoke()
PowerShell = $PowerShell
}
$Jobs.Add($JobObj) | Out-Null
}
while ($Jobs.Runspace.IsCompleted -contains $false) {
Write-Host (Get-date).Tostring() "Still running..."
Start-Sleep 1
}
}
$stopwatchScript = [system.diagnostics.stopwatch]::StartNew()
if (-not(Get-InstalledModule -Name Microsoft.graph -MinimumVersion 1.9.6)) {Install-Module microsoft.graph -scope CurrentUser -Force -AllowClobber}
Write-host "`nConnecting to Microsoft Graph API. If not authenticated already in this PowerShell session, you will be prompted for your Azure AD credentials. `n`nPlease use an account with following permissions: Organization.Read.All, User.Read.All, Directory.Read.All`n"
Connect-MgGraph -Scopes "Files.Read"
Write-host "`nConnected Successfully to Microsoft Graph API."
Select-MgProfile v1.0
Write-host "`nFetching User Drive ID"
$MGUserDriveID = (Get-MgUserDrive -UserId $Username).id
Write-host "`nUser Drive ID" $MGUserDriveID
$MGUserDriveRootChild = Get-MgUserDriveRootChild -UserId $Username -DriveId $MGUserDriveID
if (!(Test-Path $Destination)) {
New-Item -Path $Destination -ItemType Directory
}
if (Test-Path $Username'_OD4B_Folders_Report.csv') {
Remove-Item $Username'_OD4B_Folders_Report.csv'
}
if (Test-Path $Username'_OD4B_Files_Report.csv') {
Remove-Item $Username'_OD4B_Files_Report.csv'
}
Write-host -ForegroundColor White -BackgroundColor Red "`nFetching OneDrive content"
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
ForEach ($item in $MGUserDriveRootChild) {
if ($item.folder.childcount -ne $null) {
write-host $item.name is a folder
$filepath = ""
writeFoldersReport -Folder $item
ExpandOD4BFolder -Folder $item -filepath $filepath
}
else {
write-host $item.name is a file
$filepath = ""
writeFilesReport -file $item -filepath $filepath
}
}
$stopwatch.Stop()
Write-host -ForegroundColor White -BackgroundColor Red "`nFetching OneDrive content COMPLETED at" $stopwatch.Elapsed
createFolders -OD4BFoldersReport $Username'_OD4B_Folders_Report.csv' -Destination $Destination
createBatches -OD4BFilesReport $Username'_OD4B_Files_Report.csv' -ThreadCount $ThreadCount
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
downloadFiles -Username $Username -MGUserDriveID $MGUserDriveID -Destination $Destination -ThreadCount $ThreadCount
$stopwatch.Stop()
Write-host -ForegroundColor White -BackgroundColor Red "`nDownload COMPLETED at" $stopwatch.Elapsed
$stopwatchScript.Stop()
Write-host -ForegroundColor White -BackgroundColor Green "`nScript COMPLETED at" $stopwatchScript.Elapsed