forked from baswijdenes/Optimized.Mga.SharePoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Optimized.Mga.SharePoint.psm1
282 lines (260 loc) · 9.83 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
#region functions
function Get-MgaSharePointFiles {
<#
.SYNOPSIS
With Get-MgaSharePointFiles you can get a list of sharepoint files in a specific site.
.DESCRIPTION
This is the URL to sharepoint, but the tenantname (before .onmicrosoft.com) is sufficient.
.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-MgaSharePointFiles -TenantName 'BWIT.onmicrosoft.com' -Site 'Team_' -ChildFolders 'O365Reports'
#>
[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-MgaSharePointFiles: begin: Converted TenantName to $TenantName"
}
else {
Write-Verbose "Get-MgaSharePointFiles: begin: TenantName is $TenantName"
}
Write-Verbose "Get-MgaSharePointFiles: begin: Site is $Sitename"
$SPURL = 'https://graph.microsoft.com/v1.0/sites/{0}.sharepoint.com:/sites/{1}/' -f $TenantName, $Site
Write-Verbose "Get-MgaSharePointFiles: 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-MgaSharePointFiles: begin: SPItemsURL is $SPItemsURL"
$SPItems = Get-Mga -URL $SPItemsURL
}
end {
return $SPItems
}
}
function Download-MgaSharePointFiles {
<#
.SYNOPSIS
With Download-MgaSharePointFiles you can download files from a SP Site.
.DESCRIPTION
Download-MgaSharePointFiles will only work with Get-MgaSharePointFiles return.
.PARAMETER SPItem
This Parameter needs the return from Get-MgaSharePointFiles.
.PARAMETER OutputFolder
This is a FolderPath to where the files need to be exported
.EXAMPLE
foreach ($Item in $SPItems) {
Download-MgaSharePointFiles -SPItem $Item -OutputFolder 'C:\temp\'
}
#>
[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-MgaSharePointFiles: begin: $OutputFolder ends with a '\' script will trim the end"
$OutputFolder = $OutputFolder.TrimEnd('\')
}
}
process {
try {
$ContentInBytes = Invoke-WebRequest -Uri $spitem.'@microsoft.graph.downloadUrl' -usebasicparsing
Write-Verbose "Download-MgaSharePointFiles: process: retrieved $($SPItem.Name) content"
if ($OutputFolder) {
Write-Verbose "Download-MgaSharePointFiles: process: Exporting $($SPItem.Name) content"
[System.IO.file]::WriteAllBytes("$OutputFolder\$($SPItem.Name)", $ContentInBytes.content)
$Return = "Exported $($SPItem.Name) in $OutputFolder"
}
else {
Write-Verbose "Download-MgaSharePointFiles: 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-MgaSharePointFiles {
<#
.SYNOPSIS
.DESCRIPTION
Long description
.PARAMETER ItemPath
Parameter description
.PARAMETER Item
Parameter description
.PARAMETER Type
Parameter description
.PARAMETER TenantName
Parameter description
.PARAMETER Site
Parameter description
.PARAMETER ChildFolders
Parameter description
.EXAMPLE
An example
.NOTES
General notes
#>
[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-MgaSharePointFiles: begin: Converted TenantName to $TenantName"
}
else {
Write-Verbose "Upload-MgaSharePointFiles: begin: TenantName is $TenantName"
}
Write-Verbose "Upload-MgaSharePointFiles: begin: Site is $Site"
$SPURL = 'https://graph.microsoft.com/v1.0/sites/{0}.sharepoint.com:/sites/{1}/' -f $TenantName, $Site
Write-Verbose "Upload-MgaSharePointFiles: 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-MgaSharePointFiles: 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-MgaSharePointFiles: 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