5

TeamCityビルドサーバーからPowerShell3.0を使用してファイルをダウンロードしようとしています。NTLM認証を使用するようにTeamCityを構成しましたが、ファイルを直接ダウンロードしてログインにリダイレクトすることができません。

次のPowerShellコードを使用してファイルをダウンロードしようとしています。

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

リクエストからの私の応答は、ログインページへのリダイレクトです。

4

2 に答える 2

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認証をオンにしているので、ログインするために参照する特別なページがあります。そこで、ここからやりたかったのは、このログインページにアクセスし、TeamCityがCookieを使用して認証ステータスを追跡するときに取得したCookieを使用してファイルをダウンロードすることでした。

また、Invoke-WebRequestコマンドレットを使用すると、Webセッションを使用してそれらを接続することもできます。-WebSessionまたは-SessionVariableパラメーターを使用してこれを実現する方法は2つあります。試行錯誤の末、-SessionVariableパラメーターを使用すると、各リクエストの後にセッション変数が上書きされるため、実際には状態が共有されないことがわかりました。明らかに、これは私が探している動作ではありません。代わりに、-WebSessionパラメーターを使用する必要があり、ログインとファイルのダウンロードを連鎖させることができました。これを行うと、すべてが機能し始めました。

于 2013-01-09T16:46:54.957 に答える
0

-SessionVariable を使用して状態をミュート (変更) していた理由は、 -SessionVariable が、その特定の Invoke-WebReqeust Web セッションの結果を変数として出力し、後で使用できるようにするためです。

その特定のパラメーターの 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

これは、やや紛らわしい変数名のケースと見なすことができます。完成したコードをここに投稿していただきありがとうございます。

于 2014-12-17T15:37:11.533 に答える