Site warmup with Powershell
14 December, 2020 - 1 min read
I recently wanted to warmup a site running on multiple servers after deploy from Azure DevOps but i couldn't find any extensions where i could warmup several sites based on port numbers reliably.
So this powershell script will warmup as many sites as you want and it will do it in parallel so if you have caching of data at start up you don't have to wait for a site to come online before the call for next site.
# Powershell-Warmup.ps1
$urls = @(
"https://www.example.se",
"https://www.example.se:8143",
"https://www.example.se:8144",
)
workflow Invoke-URLRequest { param([parameter(Mandatory=$True)][String[]] $urls)
$myoutput=@()
ForEach -Parallel ($url in $urls)
{
$Result = Invoke-WebRequest -Method "GET" -Uri $url -TimeoutSec 600
$Workflow:myoutput += $url + ":" + $Result.StatusCode
}
Return $myoutput
}
try {
$WakeUp = Invoke-URLRequest -urls $urls -ErrorAction SilentlyContinue
$FinalResults = Invoke-URLRequest -urls $urls -ErrorAction SilentlyContinue
} catch {}
$Failed=@() ForEach ($url in $urls){
if ($FinalResults -notcontains $url + ":200") {
$Failed += $url
}
}
$OutputResults = $null if (!$Failed) {
$OutputResults = "None failed"
}
else {"Failed: " + $Failed} $OutputResults