In my working environment, we are managing more than 500+ sites. Usually, sometimes users will make a redirect to other sites or put temporary maintenance (redirect to another page) and we are not aware of their changes so we thought to validate all the sites in-frequent manner to identify those changes so thought to write post on Getting Redirected (301/302) URI’s in PowerShell.
The above scenario is very difficult to check each URL in a regular manner , it will take a lot of manual work and it may lead to human errors so we thought to automate this task instead of Manual work. The result needs to be automatically populated in Excel so we can easily share it with our Managers.
One way to get redirected URL through PowerShell is WebRequest class. In this post, we’re going to cover how to build a PowerShell function that will query a specific URL to find out if that URL is being redirected to some other URL. Here we going to discuss how to achieve this using Invoke-Webrequest.
STEP #1: First grab the response head from an Invoke-Webrequest:
$request = Invoke-WebRequest -Method Head -Uri $Url
STEP #2: Next, we need to get the Response URL using AbsoluteUri
$redirectedUri = $request.BaseResponse.ResponseUri.AbsoluteUri
Full Code : Getting Redirected (301/302) URI’s in PowerShell
This is a quick and easy way to pull the redirected URI’s for the given URI. Putting it all together we get the function below: In the Final code, I had incorporated the result in the result in Excel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
########################################################################################### #Project: Getting Redirected (301/302) URI’s in Powershell using Invoke-WebRequest Method #Developer: Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 [irp] #E-Mail: mail2thiyaguji@gmail.com ############################################################################################ function Get-RedirectedUrl { [CmdletBinding()] param ( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$GetfilePath ) $Result = @() $FormatGenerater = "<HTML><BODY background-color:grey><font color =""black""> <H2>Finiding the Redireted URLs</H2> </font><Table border=1 cellpadding=0 cellspacing=0> <TR bgcolor=gray align=center><TD><B>Source URL</B> <TD><B>RedirectedURL</TD></TR>" $filePath = $GetfilePath $fileContent = Get-Content $filePath foreach($singleURL in $fileContent ) { try { $redirectionrequest = Invoke-WebRequest -Method HEAD $singleURL -ErrorAction Ignore if ($redirectionrequest.BaseResponse.ResponseUri -ne $null) { $FormatGenerater += "<TR bgcolor=#CCFFE5>" $redirectedURL = $redirectionrequest.BaseResponse.ResponseUri.AbsoluteUri $redirectedURL } $FormatGenerater += "<TD>$($singleURL)</TD><TD>$($redirectedURL)</TD></TR>" } Catch { } } $FormatGenerater += "</Table></BODY></HTML>" $FormatGenerater | out-file C:\dotnet-helpers\RedirectedURLs.xls } Get-RedirectedUrl "C:\dotnet-helpers\URLsList.txt" |
OUTPUT:
Leave A Comment