Verify a deploy with powershell

den 1 juni 2017 av Dennie

A simple script to verify that a site and all the internal links returns nothing else than 200 ok. Best placed on a build server with an "exit 1" on fail, to break the build.

[string] $baseUrl = 'http://44tva.se'
[string] $domain = '44tva.se'
[System.Collections.ArrayList]$checkedUrls = @()

function CheckLink($url){
    if ($url -like "*$($domain)*"){

        $request = Invoke-WebRequest -Uri "$url"

        if ($request.StatusCode -eq '200') {
          write-host "OK: $url" -foregroundcolor green
        }
        else {
          write-host "Error: $url" -foregroundcolor red
        }
        
        foreach($link in $request.Links | Select href){
            if ($link.href.StartsWith('/')){
                $validLink = "http://$domain$($link.href)"

                if (-not $checkedUrls.Contains($validLink)){
                    $checkedUrls.Add($validLink)
                    CheckLink "http://$domain$($link.href)"
                }
            }
        }
    }
}

CheckLink $baseUrl