When developing web applications every now and then we have to make some scheduled task to periodically call some URL. This could be also for monitoring server uptime but I would recommend going for some 3rd party  service for that.

Our task is to call a simple GET URL every day, if possible log each  call (can be extended to log service response). Since there is no direct  way of telling windows scheduler to call web service I have found two  feasible solutions: batch file and Powershell script.

Batch file

This is the simplest solution but with shortcomings, In short we need to  create bat file somewhere on the drive with command "start {URL}" and  tell Scheduler to run the file every day. So let's go over the process.

  1. For starters I would recommend having a folder named "Schedulers"  directly in root of system drive. This will help you organize your  schedulers when you have more of them.
  2. In the designated folder create a new empty file. Right click on the  mouse-> New -> Text document which will create a new .txt document
  3. In the document paste code below replacing URL with your desired address
    'start {URL}'
  4. Now rename the file extension to .bat, if you can't see the file extensions go to View -> and check "File name extensions"
  5. Run Scheduler by clicking on Windows button and typing Scheduler
  6. "Create basic task" and follow the instructions on the screen, when you get to Action select "Start a Program", browse your bat script.
  7. That's it!!!

Pros:

  • super simple implementation
  • no code (or coding knowledge) required

Cons:

  • opens a default browser on every run
  • hard to do any logging (response logging is not possible)

PowerShell script

This is by far much more flexible solution, PowerShell is a great tool  that is fairly easy to learn and use. For this tutorial you don't need  any prior knowledge of it. In short we are going to create a small  PowerShell script which will be called by Scheduler every day and also  log all requests.

  1. As in first par of tutorial I encourage you to have a folder named "Schedulers" for all scheduled programs.
  2. Open PowerShell ISE (windows start -> type PowerShell ISE)
  3. C/P code snippet bellow to the file replacing the actual URL and log file location for your needs. Save
  4. Open Scheduler. You can just go windows start and type Scheduler
  5. In Scheduler again go through the same process to create a basic until browsing for your script in Action part.
  6. You have to put "powershell" in Program/script option and add arguments:  -file "path_to_ps_file", the interface should look like the image
$url = "SOME_URL"
$log_file = "c:\Schedulers\SOME_FILE.log"
$date = get-date -UFormat "%d/%m/%Y %R"
"$date [INFO] Executed $url" >> $log_file
 
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
$response.Close()

Pros:

  • much more flexible solution
  • doesn't open browser (you have full control over req/res)

Cons:

  • Opens PowerShell window on execution but closes after execution