如何使用PowerShell下载受保护的文件?

时间:2013-01-09 16:46:55

标签: powershell teamcity ntlm powershell-v3.0 teamcity-7.0

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

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

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

我对请求的回复是重定向到登录页面。

2 个答案:

答案 0 :(得分:12)

以下是最终解决方案的代码。


$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 参数,然后我可以将登录和文件下载链接在一起。一旦我这样做,那么一切都开始有效了。

答案 1 :(得分:0)

使用-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

这可以被视为一个有点令人困惑的变量名称的情况。感谢您在此发布完成的代码!