1

What I want to do

I want to daily ping/poke a URL to activate a action I have written in C#, and is running on a webserver. To do this I expected that I could use the task scheduler.

My setup

I have set up the scheduled tasks action to call this program:

C:\...\_resources\callurl.cmd" 

Created from this code taken from this SO post

@if (@This==@IsBatch) @then @echo off rem **** batch zone ********************************************************* setlocal enableextensions disabledelayedexpansion rem Batch file will delegate all the work to the script engine if not "%~1"=="" ( wscript //E:JScript "%~dpnx0" %1 ) rem End of batch area. Ensure batch ends execution before reaching rem javascript zone exit /b @end // **** Javascript zone ***************************************************** // Instantiate the needed component to make url queries var http = WScript.CreateObject('Msxml2.XMLHTTP.6.0'); // Retrieve the url parameter var url = WScript.Arguments.Item(0) // Make the request http.open("POST", url, false); http.send(); // All done. Exit WScript.Quit(0); 

I then add the following argument to the task

/c "http://localhost/Controller/ActionToInvoke?guid=SOME-GUID" 

The idea to use "/c" I have from this question.

The useraccount used to run the task is SYSTEM

When I do this nothing happens. Sometimes i CMD windows pops up for a split second, sometimes nothing happens. In both cases I can't see my action being hit (debugging the application and have set a breakpoint at the entry point)

Running it in CMD

If I open up my command promt, and run the command like this

callurl.cmd "http://localhost/Controller/ActionToInvoke?guid=SOME-GUID" 

there is no problem. Here callurl.cmd does what I expect it to do, it calls my action.

What can I be doing wrong? Is there something here that I don't see?

1 Answer 1

3

I would strongly recommend to do this via Powershell instead. It's way easier and perfectly works with the scheduler. Here is the code to call/trigger a URL:

$url="http://localhost/Controller/ActionToInvoke?guid=SOME-GUID" (New-Object System.Net.WebClient).DownloadString("$url"); 

Save this code in a *.ps1 file and create a task to execute it. That's it. No batch, no Java-/VBScript, etc. Just pure Powershell.

If you didn't allow the execution of ps scripts on your computer yet this is how you do this:

  1. Open Powershell as administrator
  2. execute the command set-executionpolicy remotesigned

There are different viable policies instead of remotesigned. I even prefere unrestricted instead.

EDIT: If you want to be able to pass a URL as parameter, here is the modified code:

param([String]$url="") (New-Object System.Net.WebClient).DownloadString("$url"); 

Call the file using filename.ps1 -url http://localhost/Controller/ActionToInvoke?guid=SOME-GUID.

You can also go to the task scheduler, create a new task, enter all the random stuff, go to "Actions" enter powershell into the "Program/script" field and "C:\MyScript.ps1 -url http://localhost/Controller/ActionToInvoke?guid=SOME-GUID" into the "Add arguments (optional)" field.

Sign up to request clarification or add additional context in comments.

11 Comments

I need to be able to feed the batch file with a parameter as this file will have to be used A LOT of places, all with different URL's. I donøt see how to do that with your suggestion?
@Squazz Modified my answer. Now you can pass the URL as a parameter.
trying to do this, all that happens is notepad opening the file?
Haha, I was just about to click "submit" to the exactly same edit :P
Not sure how exactly to do this but this post should answer your question stackoverflow.com/questions/17325293/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.