-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadScript.ps1
More file actions
108 lines (80 loc) · 3.74 KB
/
DownloadScript.ps1
File metadata and controls
108 lines (80 loc) · 3.74 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# PowerShell script that will download file(s) based on one or more ItemID (called by different script)
# PLEASE NOTE: This script has a dependency on another script posted to https://github.com/sharefile-org/ShareFile-PowerShell-Module
#
# This script was originally written by Joel Stocker (Twitter: @sharefilejoel)
# This sample code is provided as-is and can be used freely by others
# Last Revised: March 26, 2016
Param ( [string[]]$itemids, [string]$destDir )
# Load ShareFile PowerSHell Snap-in. The Snap-in can be found on GitHub https://github.com/sharefile-org/ShareFile-PowerShell-Module
Import-Module ShareFile
#Import-Module ShareFile-Core # For PowerShell 7+
# Load credentials from disk. If not there, challenge for credentials and store for future use
# Modify the path to match your desired location
If (test-path "$env:USERPROFILE\Documents\myauth.sfps")
{
$sfclient=Get-sfclient "$env:USERPROFILE\Documents\myauth.sfps"
}
Else
{
New-sfclient "$env:USERPROFILE\Documents\myauth.sfps"
$sfclient=get-sfclient "$env:USERPROFILE\Documents\myauth.sfps"
}
# Create empty array
$filelocations = @()
# Go through list of ItemIDs
foreach ($itemid in $itemids) {
# Get Download URL from API
$downloadlink = Send-SfRequest -Client $sfclient -Method GET -Uri "https://techteam.sf-api.com/sf/v3/Items($itemid)/Download?includeallversions=false&redirect=false" -Expand DownloadUrl
$downloadurl = $downloadlink.DownloadUrl.AbsoluteUri
# Check if the destinaton folder exist
# If it does exist, just download there
If (Test-Path $destDir) {
# Get Timestamp in epoch seconds. This will be used as part of the filename to avoid overwriting files
$timestamp = Get-Date -UFormat "%s"
# Get filename for file being downloaded
$sfItem = Send-SfRequest -Client $sfClient -Method GET -Entity Items -Id $itemid
$filename = $sfItem.FileName
$localdest = $destDir + "\" + $timestamp + "_" + $filename
# Download & Save
$start_time = Get-Date
Import-Module BitsTransfer
$job = Start-BitsTransfer -Asynchronous -Source $downloadurl -Destination $localdest
while (($Job.JobState -eq "Transferring") -or ($Job.JobState -eq "Connecting")) `
{ sleep 5;} # Poll for status, sleep for 5 seconds, or perform an action.
Switch($Job.JobState)
{
"Transferred" {Complete-BitsTransfer -BitsJob $Job}
"Error" {$Job | Format-List } # List the errors.
default {"Other action"} # Perform corrective action.
}
$filelocations += "Original Filename: " + $filename + " - Download Location: " + $localdest
# "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)" # Not being used in script
}
# If the subfolder doesn't exist, create it and start download
Else {
New-Item -Path $destDir -ItemType Directory | Out-Null
# Get Timestamp in epoch seconds. This will be used as part of the filename to avoid overwriting files
$timestamp = Get-Date -UFormat "%s"
# Get filename for file being downloaded
$sfItem = Send-SfRequest -Client $sfClient -Method GET -Entity Items -Id $itemid
$filename = $sfItem.FileName
$localdest = $destDir + "\" + $timestamp + "_" + $filename
# Download & Save
$start_time = Get-Date
Import-Module BitsTransfer
$job = Start-BitsTransfer -Asynchronous -Source $downloadurl -Destination $localdest
while (($Job.JobState -eq "Transferring") -or ($Job.JobState -eq "Connecting")) `
{ sleep 5;} # Poll for status, sleep for 5 seconds, or perform an action.
Switch($Job.JobState)
{
"Transferred" {Complete-BitsTransfer -BitsJob $Job}
"Error" {$Job | Format-List } # List the errors.
default {"Other action"} # Perform corrective action.
}
$filelocations += "Original Filename: " + $filename + " - Download Location: " + $localdest
# "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)" # Not being used in script
}
}
"`n"
"Files Downloaded:"
$filelocations