wget curl invoke-webrequest

wget / curl like functionality

$url = "http://www.google.com" $Request = Invoke-WebRequest -Uri $url -SessionVariable Session $Request = Invoke-WebRequest -Uri $url -SessionVariable Session -UserAgent $UserAgent

$Session.Cookies.GetCookies($url) $request.StatusCode $request.StatusDescription $Request.BaseResponse.ResponseUri.AbsoluteUri

$UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko)  Chrome/36.0.1944.0 Safari/537.36"  $MobileUserAgent = "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36"

Check-HTTPRedirects

function Check-HTTPRedirects { <#     .synopsis     Follow URL Redirects and display output .description     Follow 3XX Redirects untill you get a non 3XX response or MaxRedirects is reached. .PARAMETER URL     http://google.com .PARAMETER UserAgent     User Agent String .PARAMETER MaxRedirects     Maximum Redirects to follow. Default is 5 to exit loop .example     C:\PS> Check-HTTPRedirects -URL "http://google.com"     http://google.com 301 gws     http://www.google.com/ 200 gws .example     C:\PS> Check-HTTPRedirects -URL "http://google.com" -UserAgent $MobileUserAgent     http://google.com 301 gws     http://www.google.com/ 302 gws     https://www.google.com/ 200 gws #>     [CmdletBinding()]     Param(         [parameter(Mandatory = $true,HelpMessage="You must specify a URL")]         [alias("URI")]         [string] $URL=$(throw '$URL is required'),         [string] $UserAgent,         [int] $MaxRedirects = 5             )      [PSObject[]] $Responses = @()         do {         $WebRequest = $null         If ($UserAgent) {             $WebRequest = Invoke-WebRequest -Method HEAD -Uri $URL -MaximumRedirection 0 -ErrorAction Ignore -UserAgent $UserAgent         }         Else {             $WebRequest = Invoke-WebRequest -Method HEAD -Uri $URL -MaximumRedirection 0 -ErrorAction Ignore          }         #$WebRequest = Invoke-WebRequest -Method HEAD -Uri $URL -MaximumRedirection 0 -ErrorAction Ignore $(If ($UserAgent -ne $null) {"-UserAgent " + $UserAgent})                  $Counter ++         $Location = $WebRequest.Headers.Location         if ($Location -eq $null) {             $Location = $WebRequest.BaseResponse.ResponseUri.AbsoluteUri          }                  #                       #Add URL, Status Code and Server to the string         $Responses += "$URL $($WebRequest.StatusCode.ToString()) $($WebRequest.Headers.Server)"                  $URL = $Location                  #If the Status Code is not 3XX or we have reached the MaxRedirects exit the loop     } until (($WebRequest.StatusCode -lt 300 -or $WebRequest.StatusCode -gt 399) -or ($Counter -ge $MaxRedirects) )      Return $Responses }

$URI = "http://wellsfargo.com" $MobileUserAgent = "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36" Check-HTTPRedirects -URI $URI -$MaxRedirects 4 -UserAgent $MobileUserAgent

Returns

http://wellsfargo.com 301 KONICHIWA/1.1

https://www.wellsfargo.com/ 302 

https://m.wellsfargo.com/mba/?redirect=www 200 KONICHIWA/1.1