Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Created March 18, 2018 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheldonhull/830be16d464d2205236f95c7615a4446 to your computer and use it in GitHub Desktop.
Save sheldonhull/830be16d464d2205236f95c7615a4446 to your computer and use it in GitHub Desktop.
pass in csv of urls and check http status code. Sleep 10 sec in between to run in background and not hit any denials from webserver
#Requires -Modules PSFramework
$csv = Import-Csv 'C:\Users\sheldonhull\Downloads\sheldonhull-2018-03-17T07_33_03.341949-links.csv\sheldonhull-2018-03-17T07_33_03.341949-links.csv'
[Collections.Generic.List[pscustomobject]]$urls = @()
#originally from: https://stackoverflow.com/a/44234056/68698
function Get-UrlStatusCode([string] $Url)
{
try
{
(Invoke-WebRequest -Uri $Url -UseBasicParsing -DisableKeepAlive -method head).StatusCode
}
catch [Net.WebException]
{
[int]$_.Exception.Response.StatusCode
}
}
$VerbosePreference = 'silentlycontinue'
$x = 0
$csv| select -ExpandProperty url -Unique | % {
Start-Sleep -seconds 10
$statusCode = Get-UrlStatusCode $_ -verbose:$false
[void]$urls.add(
[pscustomobject]@{
URL = $_
statusCode = $statusCode
}
)
#
$minutes = ((@($csv).Count - $x++) * 10) / 60
if($statusCode -eq 404)
{$level = 'warning'}
else {$level = 'output'}
Write-PSFMessage -level $level -message ("$($x) of $(@($csv).count) - {0:n0} mins left -- $statusCode -- $($_)" -f $minutes )
[pscustomobject]@{
URL = $_
statusCode = $statusCode
} | Export-Csv -NoTypeInformation -Append c:\temp\CSV-URL.csv -encoding UTF8
}
$urls | Export-Clixml -path C:\temp\URLS.xml -force -Encoding UTF8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment