-
Notifications
You must be signed in to change notification settings - Fork 1
/
Optimized.Mga.SharePoint.psm1
323 lines (296 loc) · 11.9 KB
/
Optimized.Mga.SharePoint.psm1
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#region functions
function Get-MgaSharePointFile {
<#
.SYNOPSIS
With Get-MgaSharePointFile you can get a list of sharepoint files in a specific site.
.DESCRIPTION
With Get-MgaSharePointFile you can get a list of sharepoint files in a specific site.
Keep it mind that it will return all files in the site, use Where-Object to filter results.
.PARAMETER TenantName
This is the URL to sharepoint, but the tenantname (before .onmicrosoft.com) is sufficient
.PARAMETER Site
This is the sitename where the files are stored in.
.PARAMETER ChildFolders
Add childfolders as an array firstfolder,subfolder,subsubfolder
.EXAMPLE
$SPItems = Get-MgaSharePointFile -TenantName 'BWIT.onmicrosoft.com' -Site 'Team_' -ChildFolders 'O365Reports'
.NOTES
Author: Bas Wijdenes
.LINK
https://baswijdenes.com/how-to-download-files-from-sharepoint-with-ms-graph-api-and-powershell/
https://baswijdenes.com/how-to-upload-files-to-sharepoint-with-ms-graph-api-and-powershell/
#>
[CmdletBinding()]
param (
[Parameter(mandatory , HelpMessage = 'This is the URL to sharepoint, but the tenantname (before .onmicrosoft.com) is sufficient')]
[string]
$TenantName,
[Parameter(mandatory, HelpMessage = 'Add the sitename')]
[string]
$Site,
[Parameter(mandatory = $false, HelpMessage = 'Add childfolders as an array firstfolder,subfolder,subsubfolder')]
[string[]]
$ChildFolders
)
begin {
if ($TenantName -like '*.*') {
$TenantName = $TenantName.split('.')[0]
Write-Verbose "Get-MgaSharePointFile: begin: Converted TenantName to $TenantName"
}
else {
Write-Verbose "Get-MgaSharePointFile: begin: TenantName is $TenantName"
}
Write-Verbose "Get-MgaSharePointFile: begin: Site is $Sitename"
$SPURL = 'https://graph.microsoft.com/v1.0/sites/{0}.sharepoint.com:/sites/{1}/' -f $TenantName, $Site
Write-Verbose "Get-MgaSharePointFile: begin: SPURL is $SPURL"
$SPChildrenURL = 'https://graph.microsoft.com/v1.0/sites/{0}/drive/items/root'
$i = 1
if ($ChildFolders) {
$SPChildrenURL = 'https://graph.microsoft.com/v1.0/sites/{0}/drive/items/root:'
foreach ($ChildFolder in $ChildFolders) {
if ($i -eq $($ChildFolders).count) {
$SPChildrenURL = "$($SPChildrenURL)/$($ChildFolder):/children"
}
else {
$SPChildrenURL = "$($SPChildrenURL)/$($ChildFolder)"
}
$i++
}
}
else {
$SPChildrenURL = "$($SPChildrenURL)/children"
}
}
process {
$SPsite = Get-Mga -URL $SPURL
$SPItemsURL = $($SPChildrenURL) -f $SPSite.id
Write-Verbose "Get-MgaSharePointFile: begin: SPItemsURL is $SPItemsURL"
$SPItems = Get-Mga -URL $SPItemsURL
}
end {
return $SPItems
}
}
function Download-MgaSharePointFile {
<#
.SYNOPSIS
With Download-MgaSharePointFile you can download files from a SP Site.
.DESCRIPTION
Download-MgaSharePointFile will only work with Get-MgaSharePointFile return.
.PARAMETER SPItem
This Parameter needs the return from Get-MgaSharePointFile.
.PARAMETER OutputFolder
This is a FolderPath to where the files need to be exported
.EXAMPLE
foreach ($Item in $SPItems) {
Download-MgaSharePointFile -SPItem $Item -OutputFolder 'C:\temp\'
}
.NOTES
Author: Bas Wijdenes
.LINK
https://baswijdenes.com/how-to-download-files-from-sharepoint-with-ms-graph-api-and-powershell/
#>
[CmdletBinding()]
param (
[parameter(mandatory)]
$SPItem,
[parameter(mandatory = $true, ParameterSetName = 'OutputFolder')]
[string]
$OutputFolder
)
begin {
if (($OutputFolder) -and ($OutputFolder.Substring($OutputFolder.Length - 1, 1) -eq '\')) {
Write-Verbose "Download-MgaSharePointFile: begin: $OutputFolder ends with a '\' script will trim the end"
$OutputFolder = $OutputFolder.TrimEnd('\')
}
}
process {
try {
try {
$ContentInBytes = Invoke-WebRequest -Uri $spitem.'@microsoft.graph.downloadUrl'
}
catch {
try {
if ($_.Exception.Message -like '*Internet Explorer engine*') {
$ContentInBytes = Invoke-WebRequest -Uri $spitem.'@microsoft.graph.downloadUrl' -UseBasicParsing
}
else {
throw $_.Exception.Message
}
}
catch {
throw $_.Exception.Message
}
}
Write-Verbose "Download-MgaSharePointFile: process: retrieved $($SPItem.Name) content"
if ($OutputFolder) {
Write-Verbose "Download-MgaSharePointFile: process: Exporting $($SPItem.Name) content"
[System.IO.file]::WriteAllBytes("$OutputFolder\$($SPItem.Name)", $ContentInBytes.content)
$Return = "Exported $($SPItem.Name) in $OutputFolder"
}
else {
Write-Verbose "Download-MgaSharePointFile: process: Converting $($SPItem.Name) content to UTF8 to return"
$Return = ([System.Text.Encoding]::UTF8.GetString($ContentInBytes.content)).substring(2)
}
}
catch {
throw $_.Exception.Message
}
}
end {
return $Return
}
}
function Upload-MgaSharePointFile {
<#
.SYNOPSIS
With Upload-MgaSharePointFiles you can upload files to SharePoint sites and OneDrive.
.DESCRIPTION
With Upload-MgaSharePointFiles you can upload files to SharePoint sites and OneDrive.
.PARAMETER ItemPath
ItemPath will accept a path string to the item you want to upload to SharePoint.
.PARAMETER Item
The parameter Item will accept an item of object [System.IO.FileSystemInfo].
Which means that you first use Get-Item -Path C:\temp\blatemp.txt before you start Upload-MgaSharePointFile.
.PARAMETER Type
Type is SharePoint or OneDrive.
The default is SharePoint.
.PARAMETER TenantName
This is the URL to sharepoint, but the tenantname (before .onmicrosoft.com) is sufficient'
.PARAMETER Site
This is the sitename where the files are stored in.
.PARAMETER ChildFolders
Add childfolders as an array firstfolder,subfolder,subsubfolder
.EXAMPLE
Upload-MgaSharePointFiles -ItemPath "C:\temp\blatemp.txt" -TenantName 'm365x794103.onmicrosoft.com' -Site 'XXXX'
.NOTES
Author: Bas Wijdenes
.LINK
https://baswijdenes.com/how-to-upload-files-to-sharepoint-with-ms-graph-api-and-powershell/
#>
[CmdletBinding()]
param (
[Parameter(mandatory = $true, ParameterSetName = 'ItemPath')]
[string]
$ItemPath,
[Parameter(mandatory = $true, ParameterSetName = 'Item')]
[System.IO.FileSystemInfo]
$Item,
[Parameter(mandatory = $false)]
[ValidateSet('SharePoint', 'OneDrive')]
$Type = 'SharePoint',
[Parameter(mandatory , HelpMessage = 'This is the URL to sharepoint, but the tenantname (before .onmicrosoft.com) is sufficient')]
[string]
$TenantName,
[Parameter(mandatory, HelpMessage = 'Add the sitename')]
[string]
$Site,
[Parameter(mandatory = $false, HelpMessage = 'Add childfolders as an array firstfolder,subfolder,subsubfolder')]
[string[]]
$ChildFolders
)
begin {
if ($PSCmdlet.ParameterSetName -eq 'ItemPath') {
if ((Test-Path $ItemPath) -eq $false) {
throw "File $ItemPath cannot be found"
}
else {
$File = Get-Item $ItemPath
$LocalFileBytes = [System.IO.File]::ReadAllBytes($File)
}
}
else {
$File = $Item
$LocalFileBytes = [System.IO.File]::ReadAllBytes($File.FullName)
}
if ($TenantName -like '*.*') {
$TenantName = $TenantName.split('.')[0]
Write-Verbose "Upload-MgaSharePointFile: begin: Converted TenantName to $TenantName"
}
else {
Write-Verbose "Upload-MgaSharePointFile: begin: TenantName is $TenantName"
}
Write-Verbose "Upload-MgaSharePointFile: begin: Site is $Site"
$SPURL = 'https://graph.microsoft.com/v1.0/sites/{0}.sharepoint.com:/sites/{1}/' -f $TenantName, $Site
Write-Verbose "Upload-MgaSharePointFile: begin: SPURL is $SPURL"
$SPChildrenURL = 'https://graph.microsoft.com/v1.0/sites/{0}/drive/items/root:'
$i = 1
if ($ChildFolders) {
foreach ($ChildFolder in $ChildFolders) {
if ($i -eq $($ChildFolders).count) {
$SPChildrenURL = "$($SPChildrenURL)/$($ChildFolder)/{1}:/createUploadSession"
Write-Verbose "Upload-MgaSharePointFile: begin: ChildFolder URL is $SPChildrenURL"
}
else {
$SPChildrenURL = "$($SPChildrenURL)/$($ChildFolder)"
}
$i++
}
}
else {
$SPChildrenURL = "$($SPChildrenURL)/{1}:/createUploadSession"
}
if ($Type -eq 'OneDrive') {
$global:SPURL = $SPURL.Replace('/sites/', '/drives/')
$global:SPChildrenURL = $SPChildrenURL.Replace('/sites/', '/drives/')
$global:SPURL = $SPURL.Replace('/drive/', '')
$global:SPChildrenURL = $SPChildrenURL.Replace('/drive/', '')
}
}
process {
$SPsite = Get-Mga -URL $SPURL
$SPItemsURL = $($SPChildrenURL) -f $SPSite.id, $File.Name
Write-Verbose "Upload-MgaSharePointFile: begin: Upload URL is $SPItemsURL"
$uploadUrlResponse = Post-Mga -URL $SPItemsURL
$contentRange = [string]::Format('bytes 0-{0}/{1}', $($LocalFileBytes.Length - 1), $LocalFileBytes.Length)
$Header = @{}
$Header.Add('Content-Length', $LocalFileBytes.Length)
$Header.Add('Content-Range', $contentRange)
$Header.Add('Content-Type', 'octet/stream')
Write-Verbose $contentRange
$UploadResult = Put-Mga -URL $uploadUrlResponse.uploadUrl -InputObject $LocalFileBytes -CustomHeader $Header -Verbose
}
end {
return $UploadResult
}
}
function Get-MgaSharePointList {
[CmdletBinding()]
param (
[Parameter(mandatory, HelpMessage = 'Add the sitename')]
[string]
$Site,
[Parameter(mandatory, HelpMessage = 'Add the Listname')]
[string]
$List,
[Parameter(mandatory , HelpMessage = 'This is the URL to sharepoint, but the tenantname (before .onmicrosoft.com) is sufficient')]
[string]
$TenantName
)
begin {
Write-Verbose "Get-MgaSharePointList: begin: site: $Site"
Write-Verbose "Get-MgaSharePointList: begin: list: $List"
if ($TenantName -like '*.*') {
$TenantName = $TenantName.split('.')[0]
Write-Verbose "Get-MgaSharePointList: begin: Converted TenantName to $TenantName"
}
else {
Write-Verbose "Get-MgaSharePointList: begin: TenantName is $TenantName"
}
}
process {
$SPSiteURL = 'https://graph.microsoft.com/v1.0/sites/{0}.sharepoint.com:/sites/{1}/' -f $TenantName, $Site
$SPSite = Get-Mga -URL $SPSiteURL
$SPListURL = 'https://graph.microsoft.com/v1.0/sites/{0}/lists/{1}/items?expand=fields' -f $SPSite.id, $List
$Response = Get-Mga -URL $SPListURL
}
end {
return $Response
}
}
#endregion functions
#region aliases
New-Alias -Name 'Get-MgaSharePointFiles' -Value 'Get-MgaSharePointFile'
New-Alias -Name 'Download-MgaSharePointFiles' -Value 'Download-MgaSharePointFile'
New-Alias -Name 'Upload-MgaSharePointFiles' -Value 'Upload-MgaSharePointFile'
#endregion aliases