This PowerShell script iterates through a list of URLS from a CSV file looking for broken redirects. If any are found they are noted to a file.
#------------------------------------------------------------ cls #------------------------------------------------------------ # BEGIN: Config #------------------------------------------------------------ # Where are the files? $path = "C:\checkLinks"; # Do NOT end with \ # Source $csv = import-csv -Path "$path\source.csv"; # Report $targetFile = "report.csv"; # Where does the site send you to if a link is broken? Look at "OriginalString" in code as it's possibly not what you assume it is. $redirStr = "/error"; # What is the name of the column in the CSV file that has the URLs? $urlColumn = "url"; #------------------------------------------------------------ # END: Config #------------------------------------------------------------ #------------------------------------------------------------ # BEGIN: SUPPORTING FUNCTIONS #------------------------------------------------------------ #============================================================ # BEGIN: Get-URL # DESC: Receives params, checks URL for redirect and if redirect # matches str writes to report file. #============================================================ function Get-URL { param([string]$url) try { "Checking: $url" #======================================================== $wr = [System.Net.HttpWebRequest]::Create($url); $wrRsp = $wr.GetResponse(); $temp = $wrRsp.ResponseUri.AbsolutePath $redirectURL = $wrRsp.ResponseUri.OriginalString $redirectURL if ($temp -like $redirStr) { Add-Content $path\$targetFile "$url" }; $wrRsp.Close(); #======================================================== } catch { $errorStatus = "Exception Message: " + $_.Exception.Message; Write-Host $errorStatus; } }; #============================================================ # END: Get-URL #============================================================ #------------------------------------------------------------ # END: SUPPORTING FUNCTIONS #------------------------------------------------------------ #============================================================ # START MAIN #============================================================ #============================================================ # BEGIN: Step 1 #============================================================ # A: Go to folder cd $path # B: Does report file exist? If no, create it. if (!(Test-Path "$path\$targetFile")) { New-Item -path $path -name $targetFile -type "file" }; #============================================================ # END: Step 1 #============================================================ #============================================================ # BEGIN: Step 2 #============================================================ foreach($line in $csv) { # Loop through each row and check if URL has value. $properties = $line | Get-Member -MemberType Properties for($i=0; $i -lt $properties.Count;$i++) { $column = $properties[$i] $columnvalue = $line | Select -ExpandProperty $column.Name #----------------------------------------------------- # BEGIN: Send to check URL #----------------------------------------------------- if ($column.Name -eq $urlColumn){ if ($columnvalue.Length -gt 4){ Get-URL $columnvalue; }; }; #----------------------------------------------------- # END: Send to check URL #----------------------------------------------------- }; }; #============================================================ # END: Step 2 #============================================================