【问题标题】:How do I download a protected file using PowerShell?如何使用 PowerShell 下载受保护的文件?
【发布时间】:2012-12-23 21:48:37
【问题描述】:

我正在尝试使用 PowerShell 3.0 从我的 TeamCity 构建服务器下载文件。我已将 TeamCity 配置为使用 NTLM 身份验证,但我无法直接下载文件并重定向到登录。

我正在尝试使用以下 PowerShell 代码下载文件。

$artifacts = "http://teamcity/repository/download/bt1/.lastSuccessful/%7Bbuild.number%7D.zip"
Invoke-WebRequest -Uri $artifacts -UseDefaultCredentials

我对请求的响应是重定向到登录页面。

【问题讨论】:

    标签: powershell teamcity ntlm powershell-3.0 teamcity-7.0


    【解决方案1】:

    这是最终解决方案的代码。


    $artifacts = "http://teamcity/repository/download/bt1/.lastSuccessful/%7Bbuild.number%7D.zip"
    $login = "http://teamcity/ntlmLogin.html"
    $dest = "Artifacts.zip"
    
    $TeamCitySession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    Invoke-WebRequest -Uri $login -WebSession $TeamCitySession -UseDefaultCredentials -UseBasicParsing
    Invoke-WebRequest -Uri $artifacts -WebSession $TeamCitySession -UseBasicParsing -OutFile $dest
    

    为了弄清楚发生了什么,我需要使用 Fiddler 来跟踪成功请求的样子,并跟踪 PowerShell 中发生的事情。为了做到这一点,我必须让我的 PowerShell 请求使用它。以下是我在 PowerShell 中打开 Fiddler 跟踪的方法。

    Invoke-WebRequest -Uri $artifacts -UseDefaultCredentials -Proxy http://localhost:8888/
    

    通过将 -Proxy 参数添加到命令中,它告诉他使用 Fiddler 作为代理服务器的命令。

    从这里我看到 TeamCity 将我重定向到登录页面。由于我打开了 NTLM 身份验证,因此您可以浏览一个特殊页面以登录。所以我想从这里做的是访问这个登录页面,然后使用我返回的 cookie 下载文件,因为 TeamCity 使用 cookie 来跟踪身份验证状态。

    事实证明,Invoke-WebRequest cmdlet 还允许您使用 Web 会话连接它们。有两种方法可以使用 -WebSession-SessionVariable 参数来实现。经过反复试验,事实证明,如果您使用 -SessionVariable 参数,它将在每次请求后覆盖会话变量,因此它实际上并不共享状态。显然这不是我正在寻找的行为。相反,我必须使用 -WebSession 参数,然后我可以将登录和文件下载链接在一起。一旦我这样做了,一切就开始工作了。

    【讨论】:

    • 如果我理解正确,您可能需要将第一行Invoke-WebRequest 上的错字更正为-Uri $login。 :)
    • 谢谢@Graimer。这又是复制/粘贴问题之一。
    【解决方案2】:

    使用 -SessionVariable 的原因是静音(更改)状态是 -SessionVariable 旨在将特定 Invoke-WebReqeust websession 的结果作为变量输出,以供以后使用。

    如果我们查看该特定参数的 Get-Help:

    PS C:\windows\system32> get-help Invoke-WebRequest -Parameter SessionVariable
    
    -SessionVariable <String>
        Creates a web request session and saves it in the value of the specified variable. Enter a variable name without the dollar sign ($) symbol.
    
    When you specify a session variable, Invoke-WebRequest creates a web request session object and assigns it to a variable with the specified name in your
    Windows PowerShell session. You can use the variable in your session as soon as the command completes.
    
    Unlike a remote session, the web request session is not a persistent connection. It is an object that contains information about the connection and the
    request, including cookies, credentials, the maximum redirection value, and the user agent string. You can use it to share state and data among web
    requests.
    
    To use the web request session in subsequent web requests, specify the session variable in the value of the WebSession parameter. Windows PowerShell uses
    the data in the web request session object when establishing the new connection. To override a value in the web request session, use a cmdlet parameter,
    such as UserAgent or Credential. Parameter values take precedence over values in the web request session.
    
    You cannot use the SessionVariable and WebSession parameters in the same command.
    
    Required?                    false
    Position?                    named
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false
    

    这可以看作是变量名称有点混乱的情况。感谢您在此处发布完成的代码!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2012-09-14
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多