kjacobsen

Get-WebPage

Jan 27th, 2013
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Code Snippet from aperturescience.su
  2. Function Get-WebPage
  3. {
  4. <#
  5. .SYNOPSIS
  6. Get a webpage
  7.  
  8. .DESCRIPTION
  9. Gets the webpage at the specified URL and returns the string representation of that page. Webpages can be accessed over an URI including http://, Https://, file://, \\server\folder\file.txt etc.
  10.  
  11. .PARAMETER URL
  12. The url of the page we want to download and save as a string. URL must have format like: http://google.com, https://microsoft.com, file://c:\test.txt
  13.  
  14. .PARAMETER Credentials
  15. [Optional] Credentials for remote server
  16.  
  17. .PARAMETER WebProxy
  18. [Optional] Web Proxy to be used, if none supplied, System Proxy settings will be honored
  19.  
  20. .PARAMETER Headers
  21. [Optional] Used to specify additional headers in HTTP request
  22.  
  23. .PARAMETER UserAgent
  24. [Optional] Simpler method of specifying useragent header
  25.  
  26. .INPUTS
  27. Nothing can be piped directly into this function
  28.  
  29. .OUTPUTS
  30. String representing the page at the specified URL
  31.  
  32. .EXAMPLE
  33. Get-Webpage "http://google.com"
  34. Gets the google page and returns it
  35.  
  36. .NOTES
  37. NAME: Get-WebPage
  38. LASTEDIT: 2012-10-14 9:15:00
  39. KEYWORDS:
  40.  
  41. .LINK
  42. http://aperturescience.su/
  43. #>
  44. [CMDLetBinding()]
  45. Param
  46. (
  47.   [Parameter(mandatory=$true)] [String] $URL,
  48.   [System.Net.ICredentials] $Credentials,
  49.   [System.Net.IWebProxy] $WebProxy,
  50.   [System.Net.WebHeaderCollection] $Headers,
  51.   [String] $UserAgent
  52. )
  53.  
  54. #make a webclient object
  55. $webclient = New-Object Net.WebClient
  56. #set the pass through variables if they are not null
  57. if ($Credentials)
  58. {
  59.     $webclient.credentials = $Credentials
  60. }
  61. if ($WebProxy)
  62. {
  63.     $webclient.proxy = $WebProxy
  64. }
  65. if ($Headers)
  66. {
  67.     $webclient.headers.add($Headers)
  68. }
  69. if ($UserAgent)
  70. {
  71.     $webclient.headers["User-Agent"] = $UserAgent
  72. }
  73.  
  74. #Set the encoding type, we will use UTF8
  75. $webclient.Encoding = [System.Text.Encoding]::UTF8
  76.  
  77. #contains resultant page
  78. $result = $null
  79.  
  80. #call download string and return the string returned (or any errors generated)
  81. try
  82. {
  83.     $result = $webclient.downloadstring($URL)
  84. }
  85. catch
  86. {
  87.     throw $_
  88. }
  89.  
  90. return $result
  91.  
  92. }
  93. # Code Snippet from aperturescience.su
Advertisement
Add Comment
Please, Sign In to add comment