Sunday, 7 June 2009

Get-Cookie.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays the cookies returned from a site. 
  4. .DESCRIPTION 
  5.     This script calls System.Net.WebRequest to get a page. in the  
  6.     response there may be cookies returned which this script then 
  7.     displays. By default, the cookies returned from the MSDN home page 
  8.     are displayed. To improve production orientation, the creation of 
  9.     the request and getting the response are protected by try/catch 
  10.     blocks. 
  11. .NOTES 
  12.     File Name  : get-cookie.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell V2 CTP3 
  15.      
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.     MSDN Sample posted at: 
  20.         http://msdn.microsoft.com/en-us/library/system.net.cookie.aspx 
  21. .EXAMPLE 
  22.     PSH [C:\foo]: .\Get-Cookie.ps1 
  23.     3 Cookies returned from: http://www.microsoft.com/msdn 
  24.      
  25.     Cookie: 
  26.     A = I&I=AxUFAAAAAADmCwAA/fh06TVFlKw6GSuBKKj6Yg!!&M=1 
  27.     Domain      : .microsoft.com 
  28.     Path        : / 
  29.     Port        : 
  30.     Secure      : False 
  31.     When issued : 6/7/2009 10:06:30 AM 
  32.     Expires     : 
  33.     Expired?    : False 
  34.     Don’t save  : False
  35.     Comment     :
  36.     URI for Comments:
  37.     Version     : 0
  38.     String      : A=I&I=AxUFAAAAADmCwAA/fh06TVFlKw6GSuBKKj6Yg!!&M=1 :
  39.     Cookie:
  40.     ADS = SN=175A21EF
  41.     Domain      : .microsoft.com
  42.     Path        : /
  43.     Port        :
  44.     Secure      :
  45.     When issued : 6/7/2009 10:06:30 AM
  46.     Expires     :
  47.     Expired?    : false
  48.     Don’t save  : false  
  49.     Comment     : 
  50.     Uri for comments: 
  51.     Version     : 0 
  52.     String: ADS=SN=175A21EF : 
  53.      
  54.     Cookie: 
  55.     mediaRssUrl = http://www.microsoft.com/feeds/msdn/en-us/HDI/Home-HDI.xml 
  56.     Domain      : msdn.microsoft.com 
  57.     Path        : / 
  58.     Port        : 
  59.     Secure      : False 
  60.     When issued : 6/7/2009 10:06:30 AM 
  61.     Expires     : 
  62.     Expired?    : False 
  63.     Don’t save  : False
  64.     Comment     :
  65.     Uri for comments :
  66.     Version     : 0
  67.     String      : mediaRssUrl=http://www.microsoft.com/feeds/msdn/en-us/HDI/Home-HDI.xml :
  68. #>
  69. param (
  70. $site = “http://www.microsoft.com/msdn”
  71.    
  72. ## 
  73. # Start of script 
  74. ## 
  75.   
  76. # Create the web request object, catching any errors 
  77. try { 
  78. $request = [System.Net.WebRequest]::Create($site) 
  79. $request.CookieContainer = New-Object System.Net.CookieContainer 
  80. } 
  81. catch { 
  82. "Error creating request";$requst 
  83. return 
  84. } 
  85.   
  86. # Now get response 
  87. try { 
  88. $response = $request.GetResponse() 
  89. } 
  90. catch { 
  91. "Error getting response from $site - try later" 
  92. return 
  93. } 
  94.   
  95. # Print number of cookies 
  96. if ($response.Cookies.Count -gt 0) { 
  97. "{0} Cookies returned from: {1}" -f $Response.Cookies.Count,$site 
  98. "" 
  99. } 
  100.   
  101. # Print the properties of each cookie. 
  102. foreach ($cook in $Response.Cookies)    { 
  103. "Cookie:" 
  104. "{0} = {1}"    -f $cook.Name, $cook.Value 
  105. "Domain      : {0}"     -f $cook.Domain 
  106. "Path        : {0}"     -f $cook.Path 
  107. "Port        : {0}"     -f $cook.Port 
  108. "Secure      : {0}"     -f $cook.Secure 
  109. "When issued : {0}"     -f $cook.TimeStamp 
  110. "Expires     : {0}"     -f $cook.expireds 
  111. "Expired?    : {0}"     -f $cook.expired 
  112. "Don't save  : {0}"     -f $cook.Discard 
  113. "Comment     : {0}"     -f $cook.Comment 
  114. "Uri for comments: {0}" -f $cook.CommentUri 
  115. "Version     : {0}"     -f $cook.Version 
  116. "String: {0} :"         -f $cook.ToString() 
  117. "" 
  118. # End of Script 

5 comments:

Will said...

Hey Thomas. Looks like maybe a typo in the param block. Opens with a { instead of a ( if my young eyes aren't failing me. Thanks for the post. Very helpful for me today.

Will Steele

Thomas Lee said...

Will - thanks for the good catch. Script is updated!

Anonymous said...

Hi,

Thanks for the script!

However when I run it, I only get back 2 cookies out of 56.

Is there something I am missing?

Have a pleasant day,

Anonymous said...

Hi,

Thanks for the script!

However when I run it, I only get back 2 cookies out of 56.

Is there something I am missing?

Have a pleasant day,

Benoit COTTE

Thomas Lee said...

No idea. You might take a look at the MSDN page to see if there's anything you are doing?