Sometimes we want to know the availability of web applications. To do this, we need a system that will query specific addresses at regular intervals. But what if such a system doesn’t exist, and we need to verify it for testing purposes?
I’ve prepared a short and simple script for such needs, which, given a list of websites, queries them and logs the results to a file.
The script is written in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
$websites = @( "http://google.com", "http://wp.pl" ) $logFile = "C:\Dell\log.txt" function Test-Websites { param ( [string[]]$urls ) foreach ($website in $websites) { try { $website $startTime = Get-Date $response = Invoke-WebRequest -Uri $website -UseBasicP | Out-Null $duration = (Get-Date) - $startTime $logEntry = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $website - OK - $($duration.TotalMilliseconds) ms" } catch { $logEntry = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $website - ERROR - $_" } Add-Content -Path $logFile -Value $logEntry } } Test-Websites -urls $websites |