Created
February 18, 2017 18:35
-
-
Save eNeRGy164/ffb410e6dc6acb8610af3e4a1a85437c to your computer and use it in GitHub Desktop.
A PowerShell script that validates one or more URLs against the W3C HTML validation service. Returns info, warning and error messages. You can also switch on messages for valid pages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Validates one or more URLs against the W3C HTML validation service | |
.DESCRIPTION | |
Validates one or more URLs against the W3C HTML validation service. Returns info, warning and error messages. You can also switch on messages for valid pages. | |
.NOTES | |
Author: Michaël Hompus | |
License: This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 Generic License. | |
.PARAMETER Urls | |
One or more URLs to check for HTML validity. | |
.PARAMETER OutputValid | |
Indicates valid urls will be added to the output. | |
.PARAMETER Delay | |
Changes the deault delay between multiple request to the validation service. Default is 1000ms. | |
.LINK | |
Invoke-RestMethod | |
https://gist.github.com/eNeRGy164/ffb410e6dc6acb8610af3e4a1a85437c | |
.EXAMPLE | |
Test-HtmlValidity.ps1 -Urls https://blog.hompus.nl/ | |
.EXAMPLE | |
Test-HtmlValidity.ps1 -Urls https://blog.hompus.nl/,https://blog.hompus.nl/about/ | |
.EXAMPLE | |
Test-HtmlValidity.ps1 -Urls https://blog.hompus.nl/ -OutputValid | |
.EXAMPLE | |
Test-HtmlValidity.ps1 -Urls https://blog.hompus.nl/ -Delay 2500 | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | |
[string[]] | |
$Urls, | |
[switch] | |
$OutputValid, | |
[int] | |
$Delay = 1000 | |
) | |
BEGIN { | |
if ($null -eq $urls) { | |
$urls = @($input) | |
} | |
$first = $true | |
} | |
PROCESS { | |
Function New-OutputMessage { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $true, Position = 0)] | |
[string]$URL, | |
[Parameter(Mandatory = $true, Position = 1)] | |
[string]$Message, | |
[Parameter(Position = 2)] | |
[string]$Extract, | |
[Parameter(Position = 3)] | |
[int]$LastLine, | |
[Parameter(Position = 4)] | |
[int]$FirstColumn, | |
[Parameter(Position = 5)] | |
[int]$LastColumn, | |
[Parameter(Position = 6)] | |
[int]$HiliteStart, | |
[Parameter(Position = 7)] | |
[int]$HiliteLength | |
) | |
$msg = @{} | |
$msg.URL = $URL | |
$msg.Message = $Message | |
$msg.Extract = $Extract | |
$msg.LastLine = $LastLine | |
$msg.FirstColumn = $FirstColumn | |
$msg.LastColumn = $LastColumn | |
$msg.HiliteStart = $HiliteStart | |
$msg.HiliteLength = $HiliteLength | |
$object = New-Object –TypeName PSObject –Prop $msg | |
Write-Output $object | |
} | |
Function Write-OutputMessage { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $true, Position = 0)] | |
[ValidateSet("Valid","Error","Warning","Info")] | |
[string]$Type, | |
[Parameter(Mandatory = $true, Position = 1)] | |
[PSObject]$Message | |
) | |
# Set default layout | |
$defaultProperties = @("URL","Type","Message","Extract") | |
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet("DefaultDisplayPropertySet", [string[]]$defaultProperties) | |
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) | |
$Message | Add-Member -MemberType NoteProperty -Name Type -Value $Type | |
$Message | Add-Member MemberSet PSStandardMembers $PSStandardMembers | |
Write-Output $Message | |
} | |
$Urls | ForEach-Object { | |
$url = $_ | |
if ($first) { | |
$first = $false | |
} else { | |
Write-Verbose "Sleeping for $($Delay)ms" | |
Start-Sleep -Milliseconds $Delay | |
} | |
Write-Verbose "Validating $url" | |
$validation = Invoke-RestMethod -Uri "https://validator.nu/?doc=$([System.Net.WebUtility]::UrlEncode($url))&out=json" -UseBasicParsing | |
if ($validation.messages) { | |
$validation.messages | ForEach-Object { | |
$message = New-OutputMessage $url $_.message $_.extract $_.lastLine $_.firstColumn $_.lastColumn $_.hiliteStart $_.hiliteLength | |
if ($_.type -eq "error") { | |
Write-OutputMessage Error $message | |
} elseif ($_.subtype -eq "warning") { | |
Write-OutputMessage Warning $message | |
} else { | |
Write-OutputMessage Info $message | |
} | |
} | |
} else { | |
Write-Verbose "Valid, No messages returned" | |
if ($OutputValid) { | |
$message = New-OutputMessage $url "Valid" | |
Write-OutputMessage Valid $message | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment