Created
November 15, 2017 16:46
-
-
Save CorneliusJack/168b4cdeb129baf69af2394a2d5821dc to your computer and use it in GitHub Desktop.
PowerShell script used to download and extract the latest release zip file of a private project from the GitHub API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## PowerShell script used to download and extract the latest release zip file of a private project from the GitHub API ## | |
# Parameters | |
$orga = "[YOUR_ORGANIZATION]" | |
$repo = "[YOUR_PROJECT]" | |
$apiUrl = "https://api.github.com/repos/$orga/$repo/releases/latest" | |
$token = "[YOUR_TOKEN]" | |
$folder = "C:\[YOUR_PATH]" | |
$date = Get-Date -format "yyyy-MM-dd" | |
$zip = "$folder\$repo-$date.zip" | |
# Get the URL of the latest release from the GitHub API | |
$res = Invoke-RestMethod -Headers @{ "Authorization" = "token $token" } -Uri $apiUrl | |
$dlUrl = $res.assets.url | |
# Clean the output folder before to download and extract the zip file | |
Get-ChildItem -Path $folder -Recurse | Foreach-object {Remove-item -Recurse -path $_.FullName} | |
# Download the zip file from the latest release | |
$wc = New-Object System.Net.WebClient | |
$wc.Headers.Add("Authorization", "token $token") | |
$wc.Headers.Add("Accept", "application/octet-stream") | |
$wc.Headers.Add("user-agent", "PowerShell Script") | |
$wc.DownloadFile($dlUrl, $zip) | |
# Extract the zip file in the specified folder | |
Add-Type -AssemblyName System.IO.Compression.FileSystem | |
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried the PS extract-archive method; and it kept failing with UTF-8 encoded characters. This ps script gave me the incentive to look at the ExtractToDirectory method, and i saw an encoding attribute for the extracted filenames...
Nice script:
For those who might be interested:
[System.IO.Compression.ZipFile]::ExtractToDirectory($productZip, $temp, [System.Text.Encoding]::ascii)