-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-GoogleDriveItems.ps1
More file actions
87 lines (65 loc) · 1.97 KB
/
Get-GoogleDriveItems.ps1
File metadata and controls
87 lines (65 loc) · 1.97 KB
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
<#
.DESCRIPTION
Queries files and folders in the Google drive associated with the authenticated account
.PARAMETER ResultType
Optional filter to specify the type of results to return. Valid values are:
- All: Returns all files and folders
- Files: Returns only files
- Folders: Returns only folders
Default is All.
.PARAMETER Title
Optional filter to return files or folders with a specific title.
.PARAMETER ParentId
Optional filter to return files or folders that are children of a specific parent folder.
If not specified, returns items from the root directory.
#>
function Get-GoogleDriveItems {
[CmdletBinding()]
param(
[Parameter()]
[ValidateSet("All", "Files", "Folders")]
[string]$ResultType = "All",
[Parameter()]
[string]$Title,
[Parameter()]
[string]$ParentId
)
$q = @()
# mimeType
if ($ResultType -ne "All") {
if ($ResultType -eq "Folders") {
$q += "mimeType='application/vnd.google-apps.folder'"
}
else {
$q += "mimeType!='application/vnd.google-apps.folder'"
}
}
# title
if (![string]::IsNullOrEmpty($Title)) {
$q += "name='$Title'"
}
# parents
if (![string]::IsNullOrEmpty($ParentId)) {
$q += "'$ParentId' in parents"
}
$q += "trashed=false" # Exclude trashed items
$queryArgs = @{
q = [System.Web.HttpUtility]::UrlEncode($q -join ' and ')
pageSize = 40
}
do {
$queryString = $queryArgs.GetEnumerator() | ForEach-Object { "$($_.Name)=$($_.Value)" } | Join-String -Separator "&"
$uri = "https://www.googleapis.com/drive/v3/files?$queryString"
"Get-GoogleDriveItems: $uri" | Write-Verbose
$result = Invoke-GApi -uri $uri
# stream results
$result.files
if ('nextPageToken' -in $result.PSObject.Properties.Name) {
$queryArgs.pageToken = $result.nextPageToken
}
else {
$queryArgs.pageToken = $null
}
$result | Out-String | Write-Verbose
} while ($queryArgs.pageToken)
}