Friday, July 29, 2011

Low-budget warm-up for Web application or how to automate ping a Website

In different IT scenarios there’s a need to access a Website just to check if it is available or even to ignite it’s start.

There are some automation tools for this purpose: I’ve seen even a download from Microsoft Website for warm-up of Microsoft CRM Websites – but do not recall where.

The quick and cheap alternative is to use PowerShell script for this purpose and bind it to a scheduled task.

This is the script – quite self-explanatory

    #check and create an eventlog source to report results of website probing
$eventLogSource =
"ProbeWeb";
if (![system.diagnostics.eventlog]::SourceExists($eventLogSource))
{
new-eventlog -logname Application -source $eventLogSource
}
#create webclient
$webClient = new-object System.Net.WebClient
$output = "";
$webSite = “http://winmike.blogspot.com”;
$startTime = get-date
#probe thewebsite
$output = $webClient.DownloadString($webSite)
$endTime = get-date
#test website response for desired character sequence
if ($output -like "*Mike*")
{
$message = $webSite + " succeeded " + $startTime.DateTime + " " + ($endTime -$startTime).TotalSeconds +
" seconds"
}
else
{
$message = $webSite + " failed " + $startTime.DateTime + " " + ($endTime - $startTime).TotalSeconds +
" seconds"
}
#record the results
write-eventlog -logname Application -source $eventLogSource -eventID 42
-message $message
Save this script in a file on the disk (for exampe, in c:\scripts\probeweb.ps1).
Start the script from powershell
x10sctmp0
and you will see the output in the eventlog - smth like that:
x10sctmp
Well, now bind it to a scheduled task. Open the Task Scheduler Library (via computer management) and select “Create Basic Task…”:
x10sctmp1
Enter task name and description, then select time period to repeat the task:
x10sctmp2 x10sctmp3
Select “start a program! as a task action:
x10sctmp4
Enter program name as follows
c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe
(check the %SystemRoot% – your drive letter and path may vary)
and
c:\scripts\probewe.ps1
as argument (or alternate path to your script):
x10sctmp5
Review summary of the task:
x10sctmp6
…and confirm the task. You will see the task in the list of your task library:
x10sctmp7
Open the task properties and assure “Run whether user is logged on or not” is selected and the correct user account is used as execution context.
x10sctmp9
Confirm the password if requested.
Run newly created task to make sure it works:
x10sctmp10
Analyze the results
x10sctmp11
…and enjoy!

No comments: