PowerShellのInvoke-WebRequestでCookieを取得して使う

例えばGoogleからのCookieを取得してみます。SessionVariableの引数がポイントです。

$uri = "http://www.google.co.jp/"
Invoke-WebRequest -Uri $uri -SessionVariable mySession

これで変数$mySessionにセッション情報が格納されます。

PS> $mySession | gm
   TypeName: Microsoft.PowerShell.Commands.WebRequestSession

Name                  MemberType Definition                                                                                     
----                  ---------- ----------                                                                                     
Equals                Method     bool Equals(System.Object obj)                                                                 
GetHashCode           Method     int GetHashCode()                                                                              
GetType               Method     type GetType()                                                                                 
ToString              Method     string ToString()                                                                              
Certificates          Property   System.Security.Cryptography.X509Certificates.X509CertificateCollection Certificates {get;set;}
Cookies               Property   System.Net.CookieContainer Cookies {get;set;}                                                  
Credentials           Property   System.Net.ICredentials Credentials {get;set;}                                                 
Headers               Property   System.Collections.Generic.Dictionary[string,string] Headers {get;set;}                        
MaximumRedirection    Property   int MaximumRedirection {get;set;}                                                              
Proxy                 Property   System.Net.IWebProxy Proxy {get;set;}                                                          
UseDefaultCredentials Property   bool UseDefaultCredentials {get;set;}                                                          
UserAgent             Property   string UserAgent {get;set;}                    

 
 

Cookieの中身も確認できます。

PS> $mySession.Cookies.GetCookies($uri)
Comment    : 
CommentUri : 
HttpOnly   : False
Discard    : False
Domain     : .google.co.jp
Expired    : False
Expires    : 2017/03/01 23:04:45
Name       : PREF
Path       : /
Port       : 
Secure     : False
TimeStamp  : 2015/03/02 23:04:45
Value      : ID=xxxxx:FF=0:TM=11111:LM=11111:S=XXXXXXX
Version    : 0
...

 
 
他のリクエストでセッション情報を使いたいならWebSessionの引数にセッション情報の入った変数を渡します。

Invoke-WebRequest -Uri $uri -WebSession $mySession


Export-Csvなどで保存したCookie情報を後から使いたい場合は、SessionオブジェクトとCookieオブジェクトを新規に作成して、Cookieオブジェクトに保存した情報を設定して使ったりもできます。

$savedCookieData = Import-Csv C:\cookie.csv
$myUri = "http://somesite.com"
$mySession = New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession
$myCookie = New-Object -TypeName System.Net.Cookie
#必須のNameを設定する
$myCookie.Name = "something"
$myCookie.Value = $savedCookieData.Value
$mySession.Cookies.Add($myUri,$myCookie)
Invoke-WebRequest -Uri $uri -WebSession $mySession