-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrash-search-automation.ps1
executable file
·174 lines (142 loc) · 6.67 KB
/
trash-search-automation.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
# Set to a specific Box user id if you would like to search the trash of a specific user instead of the service account
$AsUserId = $null
# File name to filter by
$ItemName = "my_file_name"
# Login email to filter by
$CreatedByLogin = "[email protected]"
# Option to restore the file to a specific folder
$RestoreFile = $False
$RestoreParentFolderId = "ADD_PARENT_FOLDER_ID"
# CSV Report params
$CsvReportPath = "./reports"
$Limit = "100"
$Offset = 0
$ItemCount = 1
# Main function
Function Start {
Write-Output "Searching trashed items by name..."
# Search-Trashed-Items-By-FileName
$script:Offset = 0
$script:ItemCount = 1
Write-Output "Searching trashed items by name and created_by.login..."
# Search-Trashed-Items-By-FileName-And-CreatedBy
}
Function Search-Trashed-Items-By-FileName {
Write-Output "Using offset: $($Offset)"
# Fields to include in the results
$Fields = "id,name,type"
# Query String for the search trashed items request. Check if the As-User Id is populated
$QueryString = "query=$($ItemName)&fields=$($Fields)&trash_content=trashed_only&type=file&offset=$($Offset)&limit=$($Limit)"
If($AsUser -ne $null) {
$Response = box request /search `
--method=GET `
--query=$QueryString `
--as-user=$AsUserId | ConvertFrom-Json | % body
}
Else {
$Response = box request /search `
--method=GET `
--query=$QueryString | ConvertFrom-Json | % body
}
Write-Output "Found response body: $($Response)"
$TotalCount = $Response.total_count
Write-Output "Found total count: $($TotalCount)"
$TrashedItems = $Response.entries
Write-Output "Found trashed items: $($TrashedItems.Length)"
$script:Offset += $TrashedItems.Length
Write-Output "Set new offset: $($script:Offset)"
# Loop through the trashed items
ForEach($Item in $TrashedItems) {
$ItemCreatedBy = $Item.created_by.login
Write-Output "$($ItemCount) - Found item with name: $($Item.name), id: $($Item.id), and type: $($Item.type)"
# Check for a partial match the item name in the results
If($Item.type -eq "file" -AND
$Item.name -Match $ItemName) {
Write-Output "$($ItemCount) - Found MATCHING item with name: $($Item.name), id: $($Item.id), and type: $($Item.type)"
# We found an item, so create an object and append it to a CSV file
[PSCustomObject]@{
Name = $Item.name;
Id = $Item.id;
Type = $Item.type
} | Export-Csv -Path $CsvReportPath/trashed_items_name.csv -Append -NoTypeInformation -Force
# Check if we need to restore the file since we cant download directly from the trash
If($RestoreFile -eq $True) {
# Send post request to restore file from trash
$AccessToken = box tokens:get
$RestoreResponse = Invoke-WebRequest -Uri "https://api.box.com/2.0/files/$($Item.id)" `
-Method "POST" `
-ContentType "application/json" `
-Headers @{ "Authorization" = "Bearer $($AccessToken)" } | ConvertFrom-Json
# Move item to another parent folder
box files:move $Item.id $RestoreParentFolderId
Write-Output "Restored file with id: $($Item.id) and moved to folder: $($RestoreParentFolderId)"
}
}
$script:ItemCount++
}
# Only continue if the Offset is not equal to the total items in the trash
If($Offset -lt $TotalCount) {
Search-Trashed-Items-By-FileName
}
}
Function Search-Trashed-Items-By-FileName-And-CreatedBy {
Write-Output "Using offset: $($Offset)"
# Fields to include in the results
$Fields = "id,name,type,created_by"
# Query String for the search trashed items request. Check if the As-User Id is populated
$QueryString = "query=$($ItemName)&fields=$($Fields)&trash_content=trashed_only&type=file&offset=$($Offset)&limit=$($Limit)"
If($AsUser -ne $null) {
$Response = box request /search `
--method=GET `
--query=$QueryString `
--as-user=$AsUserId | ConvertFrom-Json | % body
}
Else {
$Response = box request /search `
--method=GET `
--query=$QueryString | ConvertFrom-Json | % body
}
Write-Output "Found response body: $($Response)"
$TotalCount = $Response.total_count
Write-Output "Found total count: $($TotalCount)"
$TrashedItems = $Response.entries
Write-Output "Found trashed items: $($TrashedItems.Length)"
$script:Offset += $TrashedItems.Length
Write-Output "Set new offset: $($script:Offset)"
# Loop through the trashed items
ForEach($Item in $TrashedItems) {
$ItemCreatedBy = $Item.created_by.login
Write-Output "$($ItemCount) - Found item with name: $($Item.name), id: $($Item.id), type: $($Item.type), and created_by login: $($ItemCreatedBy)"
# Check for a PARTIAL match in the item name AND EXACT match in Created By Login in the results
If($Item.type -eq "file" -AND
$Item.name -Match $ItemName -AND
$ItemCreatedBy -eq $CreatedByLogin) {
Write-Output "$($ItemCount) - Found MATCHING item with name: $($Item.name), id: $($Item.id), type: $($Item.type), and created_by login: $($ItemCreatedBy)"
# We found an item, so create an object and append it to a CSV file
[PSCustomObject]@{
Name = $Item.name;
Id = $Item.id;
Type = $Item.type;
CreatedBy = $Item.created_by.login
} | Export-Csv -Path $CsvReportPath/trashed_items_createdby.csv -Append -NoTypeInformation -Force
# Check if we need to restore the file since we cant download directly from the trash
If($RestoreFile -eq $True) {
# Send post request to restore file from trash
$AccessToken = box tokens:get
$RestoreResponse = Invoke-WebRequest -Uri "https://api.box.com/2.0/files/$($Item.id)" `
-Method "POST" `
-ContentType "application/json" `
-Headers @{ "Authorization" = "Bearer $($AccessToken)" } | ConvertFrom-Json
# Move item to another parent folder
box files:move $Item.id $RestoreParentFolderId
Write-Output "Restored file with id: $($Item.id) and moved to folder: $($RestoreParentFolderId)"
}
}
$script:ItemCount++
}
# Only continue if the Offset is not equal to the total items in the trash
If($Offset -lt $TotalCount) {
Search-Trashed-Items-By-FileName-And-CreatedBy
}
}
Start