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. ## 
  74. # Start of script 
  75. ## 
  76.   
  77. # Create the web request object, catching any errors 
  78. try { 
  79. $request = [System.Net.WebRequest]::Create($site) 
  80. $request.CookieContainer = New-Object System.Net.CookieContainer 
  81. } 
  82. catch { 
  83. "Error creating request";$requst 
  84. return 
  85. } 
  86.   
  87. # Now get response 
  88. try { 
  89. $response = $request.GetResponse() 
  90. } 
  91. catch { 
  92. "Error getting response from $site - try later" 
  93. return 
  94. } 
  95.   
  96. # Print number of cookies 
  97. if ($response.Cookies.Count -gt 0) { 
  98. "{0} Cookies returned from: {1}" -f $Response.Cookies.Count,$site 
  99. "" 
  100. } 
  101.   
  102. # Print the properties of each cookie. 
  103. foreach ($cook in $Response.Cookies)    { 
  104. "Cookie:" 
  105. "{0} = {1}"    -f $cook.Name, $cook.Value 
  106. "Domain      : {0}"     -f $cook.Domain 
  107. "Path        : {0}"     -f $cook.Path 
  108. "Port        : {0}"     -f $cook.Port 
  109. "Secure      : {0}"     -f $cook.Secure 
  110. "When issued : {0}"     -f $cook.TimeStamp 
  111. "Expires     : {0}"     -f $cook.expireds 
  112. "Expired?    : {0}"     -f $cook.expired 
  113. "Don't save  : {0}"     -f $cook.Discard 
  114. "Comment     : {0}"     -f $cook.Comment 
  115. "Uri for comments: {0}" -f $cook.CommentUri 
  116. "Version     : {0}"     -f $cook.Version 
  117. "String: {0} :"         -f $cook.ToString() 
  118. "" 
  119. # End of Script 

0 comments: