Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the code for the <strong>final solution</strong>.</p> <hr> <pre><code>$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 </code></pre> <hr> <p>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.</p> <pre><code>Invoke-WebRequest -Uri $artifacts -UseDefaultCredentials -Proxy http://localhost:8888/ </code></pre> <p>By adding the <em>-Proxy</em> argument to the command it told he command to use Fiddler as a proxy server.</p> <p>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.</p> <p>It also turns out that the <em>Invoke-WebRequest</em> cmdlets also allow you to connect them using a web session. There are two ways of accomplishing this using either the <em>-WebSession</em> or the <em>-SessionVariable</em> parameter. After some trial and error it turns out that if you use the <em>-SessionVariable</em> 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 <em>-WebSession</em> parameter and then I could chain together the login and the download of the file. Once I did this then everything started working.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload