Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 私有Git回购-异常调用";下载文件;加上;2“;论点:“;请求被中止:连接意外关闭;_Powershell_Github - Fatal编程技术网

Powershell 私有Git回购-异常调用";下载文件;加上;2“;论点:“;请求被中止:连接意外关闭;

Powershell 私有Git回购-异常调用";下载文件;加上;2“;论点:“;请求被中止:连接意外关闭;,powershell,github,Powershell,Github,目前正在尝试从Powershell中我的一个私有Git存储库下载文件,以下是我的代码: [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $wc = New-Object -TypeName System.Net.WebClient; $wc.Headers.Add('Authorization','token ------------my token------------');

目前正在尝试从Powershell中我的一个私有Git存储库下载文件,以下是我的代码:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; 
$wc = New-Object -TypeName System.Net.WebClient; 
$wc.Headers.Add('Authorization','token ------------my token------------'); 
$wc.DownloadFile('https://github.com/Private/Repository/blob/master/hellothere.bat', '%~dp0hello_there.bat');
我对powershell非常陌生,所以不确定这是否是实现这一点的方法,但到目前为止,我一直在尝试从公共Git存储库下载。我假设这是由于某种身份验证问题造成的,因为连接正在关闭,但是我看不出语法有什么问题,而且我肯定使用了正确的令牌


非常感谢您的帮助。这里有两个主要问题需要解决:

  • 授权
    标题需要采用以下格式:
  • 该问题与GitHub返回的404未找到相关:
  • 要解决此问题,必须确保获得文件原始内容的URL,如下所示:

    那它就可以正常工作了。最后的代码与您的代码相同,只是调整了标题

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; 
    $wc = New-Object -TypeName System.Net.WebClient; 
    $wc.Headers.Add('Authorization','bearer $token '); 
    $wc.DownloadFile('https://github.com/path/to/raw/file', '/path/to/output');
    
    更新 如果您想下载二进制文件,那么URL必须稍微更改,因为二进制文件存储在其他地方。请注意,未来的读者,这可能会改变

    目前我的格式是:

    https://raw.githubusercontent.com/$USERNAME/$REPO\u NAME/$BRANCH\u NAME/$BINARY\u NAME

    或者在我的情况下,它是:


    https://raw.githubusercontent.com/JuxhinDB/Test/master/Test.exe

    我在使用上述方法时也遇到了很多问题,可能是由于Github API中的更改等原因

    幸运的是,经过无数次的尝试,在几个星期的过程中,我最终找到了一些可行的解决方案

    如果您只想将文件从专用Github存储库下载到硬盘驱动器上的特定位置,可以使用以下方法

    $ghtoken = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $baseurl = "https://raw.githubusercontent.com/$USERNAME/$REPO_NAME/master/$PATH"
    $tokenheaders = @{"Authorization"="token $($ghtoken)";"Accept"= "application/vnd.github.v3.raw"}
    $scripturl="$($baseurl)/Test.exe"
    Invoke-WebRequest -Uri $scripturl -Headers $tokenheaders -UseBasicParsing -OutFile "C:\Temp\Test.exe"
    
    $ghtoken = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $baseurl = "https://raw.githubusercontent.com/$USERNAME/$REPO_NAME/master/$PATH"
    $tokenheaders = @{"Authorization"="token $($ghtoken)";"Accept"= "application/vnd.github.v3.raw"}
    $scripturl="$baseurl/Script_To_Run.ps1"
    Invoke-Expression(Invoke-WebRequest -Uri $scripturl -Headers $tokenheaders -UseBasicParsing | Select-Object -ExpandProperty Content)
    
    但是,如果您试图从Github存储库运行脚本(如PowerShell脚本),而不必下载文件,请尝试以下操作

    $ghtoken = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $baseurl = "https://raw.githubusercontent.com/$USERNAME/$REPO_NAME/master/$PATH"
    $tokenheaders = @{"Authorization"="token $($ghtoken)";"Accept"= "application/vnd.github.v3.raw"}
    $scripturl="$($baseurl)/Test.exe"
    Invoke-WebRequest -Uri $scripturl -Headers $tokenheaders -UseBasicParsing -OutFile "C:\Temp\Test.exe"
    
    $ghtoken = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $baseurl = "https://raw.githubusercontent.com/$USERNAME/$REPO_NAME/master/$PATH"
    $tokenheaders = @{"Authorization"="token $($ghtoken)";"Accept"= "application/vnd.github.v3.raw"}
    $scripturl="$baseurl/Script_To_Run.ps1"
    Invoke-Expression(Invoke-WebRequest -Uri $scripturl -Headers $tokenheaders -UseBasicParsing | Select-Object -ExpandProperty Content)
    

    我希望这有助于其他人,他们可能会遇到同样的问题,等等。

    您的输出文件打算在其文件名中包含
    %~
    ?不,这样它就可以将文件下载到运行脚本的同一目录中。源URL也是404s(不存在)。这是您试图下载的文字URL吗?是的,我正在尝试访问一个实际的URL,我可以确认它是可访问的。这只是一个虚拟URL。这也适用于前任吗?它使用的是bat文件,但到目前为止还没有使用EXE。我相信它已经停止工作了,因为我似乎无法再从我的私人git repo下载了。URL绝对正确,我不断收到404未找到错误。