如何使用 PowerShell 下载受保护的文件? [英] How do I download a protected file using PowerShell?
问题描述
我正在尝试使用 PowerShell 3.0 从我的 TeamCity 构建服务器下载文件.我已将 TeamCity 配置为使用 NTLM 身份验证,但无法直接下载文件并重定向到登录.
I am trying to download a file using PowerShell 3.0 from my TeamCity build server. I have configured TeamCity to use NTLM authentication but I cannot download the file directly and get redirected to login.
我正在尝试使用以下 PowerShell 代码下载文件.
I am trying to use the following PowerShell code to download the file.
$artifacts = "http://teamcity/repository/download/bt1/.lastSuccessful/%7Bbuild.number%7D.zip"
Invoke-WebRequest -Uri $artifacts -UseDefaultCredentials
我对请求的响应是重定向到登录页面.
My response from the request is a redirection to the login page.
推荐答案
这是最终解决方案的代码.
$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 跟踪.
In order to figure out what was happening I needed to use Fiddler to trace what a successful request looks like and also trace what was happening in PowerShell. In order to do that I had to make my PowerShell request use it. The following is how I turned on Fiddler tracing from within PowerShell.
Invoke-WebRequest -Uri $artifacts -UseDefaultCredentials -Proxy http://localhost:8888/
通过在命令中添加 -Proxy 参数,它告诉他命令使用 Fiddler 作为代理服务器.
By adding the -Proxy argument to the command it told he command to use Fiddler as a proxy server.
从这里我看到 TeamCity 将我重定向到登录页面.由于我打开了 NTLM 身份验证,因此您可以浏览一个特殊页面以登录.所以我想从这里做的是访问这个登录页面,然后使用我返回的 cookie 下载文件,因为 TeamCity 使用 cookie 来跟踪身份验证状态.
From here I saw that TeamCity was redirecting me to the login page. Since I have NTLM authentication turned on there is a special page that you browse to in order to login. So what I wanted to do from here was to visit this login page and then download the file using the cookies that I get back as TeamCity uses a cookie to track authentication status.
事实证明,Invoke-WebRequest cmdlet 还允许您使用 Web 会话连接它们.有两种方法可以使用 -WebSession 或 -SessionVariable 参数来完成此操作.经过一些试验和错误,结果证明如果您使用 -SessionVariable 参数,它将在每次请求后覆盖会话变量,因此它实际上不会共享状态.显然,这不是我正在寻找的行为.相反,我必须使用 -WebSession 参数,然后我可以将登录和文件下载链接在一起.一旦我这样做了,一切就开始工作了.
It also turns out that the Invoke-WebRequest cmdlets also allow you to connect them using a web session. There are two ways of accomplishing this using either the -WebSession or the -SessionVariable parameter. After some trial and error it turns out that if you use the -SessionVariable parameter it will overwrite the session variable after each request, so that it doesn't actually share the state. Clearly this is not the behavior I am looking for. Instead I had to use the -WebSession parameter and then I could chain together the login and the download of the file. Once I did this then everything started working.
这篇关于如何使用 PowerShell 下载受保护的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!