Downloading a file from github enterprise (authenticated/ssl) on Windows using Powershell

By thomas, 20 February, 2015

I need to download a script from github but I don't have git on the windows machines, on Linux I just used curl -u, for windows it needed more than a one liner.
Here's what I came up with, we have self signed certs so I need to fool System.Net into thinking all certs are good, that's a one liner:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Next I create a WebClient object and set two headers:

$client = New-Object System.Net.WebClient
$client.Headers.Add("Authorization","token 1234567890notarealtoken987654321")
$client.Headers.Add("Accept","application/vnd.github.v3.raw")

I set up an OAuth token for github earlier, so I set that as my authorization to login to github enterprise. The second header tells github that I'm ok with raw files (if you don't do this, you get back a json)

Finally, I download the file.

$client.DownloadFile("https://github.company.com/api/v3/repos/me/my_repo/contents/test/test-script.ps1?ref=branch_I_need","C:\temp\test-script.ps1”)

You can probably roll this up into a one liner and get rid of the $client object, but this is much more readable. Here's everything together.


[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$client = New-Object System.Net.WebClient
$client.Headers.Add("Authorization","token 1234567890notarealtoken987654321")
$client.Headers.Add("Accept","application/vnd.github.v3.raw")
$client.DownloadFile("https://github.company.com/api/v3/repos/me/my_repo/contents/test/test-script.ps1?ref=branch_I_need","C:\temp\test-script.ps1”)

A little more work than

curl -u me -k https://github.company.com/api/v3/repos/me/my_repo/contents/test/test-script.ps1 >test-script.ps1