-
-
Save chrisbrownie/f20cb4508975fb7fb5da145d3d38024a to your computer and use it in GitHub Desktop.
function DownloadFilesFromRepo { | |
Param( | |
[string]$Owner, | |
[string]$Repository, | |
[string]$Path, | |
[string]$DestinationPath | |
) | |
$baseUri = "https://api.github.com/" | |
$args = "repos/$Owner/$Repository/contents/$Path" | |
$wr = Invoke-WebRequest -Uri $($baseuri+$args) | |
$objects = $wr.Content | ConvertFrom-Json | |
$files = $objects | where {$_.type -eq "file"} | Select -exp download_url | |
$directories = $objects | where {$_.type -eq "dir"} | |
$directories | ForEach-Object { | |
DownloadFilesFromRepo -Owner $Owner -Repository $Repository -Path $_.path -DestinationPath $($DestinationPath+$_.name) | |
} | |
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) | |
try { | |
Invoke-WebRequest -Uri $file -OutFile $fileDestination -ErrorAction Stop -Verbose | |
"Grabbed '$($file)' to '$fileDestination'" | |
} catch { | |
throw "Unable to download '$($file.path)'" | |
} | |
} | |
} |
Working like a charm, well done!
I know it's been three years, but I forked your git, and added authentication (to increase API limit) and some minor tweaks.
Would love to hear what you think. Gist Fork
Tried this script for download a particular file kept at private repo but did not work. It ran immediately and did not show execute any exception scenarios as well. Is there any environment pre-requisites that needs to be met or am I missing something here ?
How will this work if the repository is private? Is there anyone to stick in the oauth token?
I need help with this too.
If the repo is private...
$token = "<Create a Personal Access Token>"
$headers = @{Authorization = "token $($token)"}
Invoke-WebRequest -Headers $headers -Uri $uri
If the repo is private...
$token = "<Create a Personal Access Token>" $headers = @{Authorization = "token $($token)"} Invoke-WebRequest -Headers $headers -Uri $uri
Thank you @StanleyGoldman , So difficult to find this specific scenario cleanly presented.
I am new on GitHub. This article was very useful. Thank you very much.
How will this work if the repository is private? Is there anyone to stick in the oauth token?