-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadFilesFromRepo.ps1
47 lines (42 loc) · 1.87 KB
/
DownloadFilesFromRepo.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
#Other implementation: https://gist.github.com/zerotag/cfc7d57eef5df9ae29ef8a56a367e6dc
function DownloadFilesFromRepo {
Param(
[string]$Owner,
[string]$Repository,
[string]$Path,
[string]$DestinationPath,
[string]$Pat
)
# Setup
$baseUri = "https://api.github.com/"
$uri = "repos/$Owner/$Repository/contents/$Path"
$headers = @{Authorization = "token $($Pat)"}
$wr = Invoke-WebRequest -Uri $($baseuri+$uri) -Headers $headers
$objects = $wr.Content | ConvertFrom-Json
$files = $objects | Where-Object {$_.type -eq "file"} | Select-Object -exp download_url
$directories = $objects | Where-Object {$_.type -eq "dir"}
$directories | ForEach-Object {
DownloadFilesFromRepo -Owner $Owner -Repository $Repository -Path $_.path -DestinationPath $($DestinationPath+$_.name) -Pat $Pat
}
if (-not (Test-Path $DestinationPath)) {
# Destination path does not exist, let's create it
try {
New-Item -Path $DestinationPath -ItemType Directory -ErrorAction Stop
} catch {
throw "Could not create path '$DestinationPath'!"
}
}
foreach ($file in $files) {
$fileDestination = Join-Path $DestinationPath (Split-Path $file -Leaf)
if($fileDestination.Contains("?token")) {
$locationOfToken = $fileDestination.IndexOf("?token")
$fileDestination = $fileDestination.Substring(0, $locationOfToken);
}
try {
Invoke-WebRequest -Uri $file -OutFile $fileDestination -Headers $headers -ErrorAction Stop -Verbose
"Grabbed '$($file)' to '$fileDestination'"
} catch {
throw "Unable to download '$($file.path)'"
}
}
}