How to download latest private GitHub release with PowerShell script?

Hello,

I recently wrote a PowerShell script that downloads the latest release from a public repo and that works as intended. However, I want to change my script so it can access my private repo. Here is the code I have tried so far:

# Download latest release from GitHub
$credentials="myPersonalAccessToken"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "token $credentials")

$repo = "myUserName/MyPrivateReleaseRepo"
$file = "MyBinaries.zip"

$releases = "https://api.github.com/repos/$repo/releases"

Write-Host Determining latest release
$tag = (Invoke-WebRequest $releases -Headers $headers | ConvertFrom-Json)[0].tag_name

$download = "https://github.com/$repo/releases/download/$tag/$file"
$name = $file.Split(".")[0]
$zip = "$name-$tag.zip"
$dir = "$name-$tag"

Write-Host Dowloading latest release
Invoke-WebRequest $download -Out $zip

Write-Host Extracting release files
Expand-Archive $zip -Force

# Cleaning up target dir
Remove-Item "C:\MyOutPutFolder\$name" -Recurse -Force -ErrorAction SilentlyContinue 

# Moving from temp dir to target dir
Move-Item $dir\$name -Destination "C:\MyOutPutFolder\$name" -Force

# Removing temp files
Remove-Item $zip -Force
Remove-Item $dir -Recurse -Force

I get the following error when testing on my private repo:

Invoke-WebRequest : Not Found
At C:\Script\DownloadLatestGitHubRelease.ps1:25 char:1
+ Invoke-WebRequest $download -Out $zip
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Am I using the access token incorrectly? As I said above the script works perfectly fine on my public repo and fails on my private repo.

Hello there @bmils

I found a solution !

Simple version (without tag and folder): Download assets from private Github releases using PowerShell · GitHub

Version similar to your script:

# Download latest release from GitHub
$credentials="<github_access_token>"
$repo = "<user_or_org>/<repo_name>"
$file = "<name_of_asset_file>"
$folder="<folder_to_extract_to>"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "token $credentials")
$releases = "https://api.github.com/repos/$repo/releases"

Write-Host Determining latest release
$id = ((Invoke-WebRequest $releases -Headers $headers | ConvertFrom-Json)[0].assets | where { $_.name -eq $file })[0].id
$tag = (Invoke-WebRequest $releases -Headers $headers | ConvertFrom-Json)[0].tag_name

$download = "https://" + $credentials + ":@api.github.com/repos/$repo/releases/assets/$id"
$name = $file.Split(".")[0]
$zip = "$name-$tag.zip"
$dir = "$name-$tag"

Write-Host Dowloading latest release
$headers.Add("Accept", "application/octet-stream")
$zip = "$name-$tag.zip"
Invoke-WebRequest -Uri $download -Headers $headers -OutFile $zip

Write-Host Extracting release files
Expand-Archive $zip -Force

# Cleaning up target dir
Remove-Item "$folder\$name" -Recurse -Force -ErrorAction SilentlyContinue 

# Moving from temp dir to target dir
Move-Item $dir\$name -Destination "$folder\$name" -Force

# Removing temp files
Remove-Item $zip -Force
Remove-Item $dir -Recurse -Force

The Linux version helped me a lot: Download assets from private Github releases · GitHub

1 Like