Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Created March 18, 2018 21:20
Embed
What would you like to do?
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