PowerShell : retrieve file from GitHub

拟墨画扇 提交于 2020-12-01 12:52:21

问题


I need to download a file from my GitHub private repo. So following the instructions on the GitHub site, I created an OAuth token for my credentials.

Then I executed this PS script :

$WebClient = New-Object -TypeName System.Net.WebClient
$WebClient.Headers.Add('Authorization','{OAuth token}')
$uri = "https://github.com/mycompany/myrepo/blob/master/myfile.zip"
$targetPath = "c:\temp"
$WebClient.DownloadFile($uri, $targetPath)

However, $WebClient.DownloadFile() returns a 404. This is strange because I can retrieve the file from $uri via a browser logged-in to GitHub with same credentials used to create OAuth token.


回答1:


According to this your two options are HTTPS basic auth and an OAuth token.

So to add basic auth to your webclient try this:

$url = 'https://github.com/mycompany/myrepo/blob/master/myscript.ps1'
$wc = New-Object -TypeName System.Net.WebClient
$wc.Credentials = New-Object -TypeName System.Net.NetworkCredential 'username', 'password'
iex ($wc.DownloadString($url))

To use OAuth you'll need to add a header named Authorization and provide the token string as an argument. Replace the NetworkCredential line from the example above with this.

$wc.Headers.Add('Authorization','token your_token')

Follow the instructions here to create the OAuth token using curl. This part can be done with PowerShell but it's only a one time thing so you can just use the example GitHub provides.



来源:https://stackoverflow.com/questions/16597874/powershell-retrieve-file-from-github

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!