Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save garyzava/7a6c88550d7b6c33657f03f730f31d57 to your computer and use it in GitHub Desktop.
Save garyzava/7a6c88550d7b6c33657f03f730f31d57 to your computer and use it in GitHub Desktop.
PowerShell function to download files from a GitHub repository
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)'"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment