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?

Community
  • 1
  • 1
Squazz
  • 3,912
  • 7
  • 38
  • 62

1 Answers1

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.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • 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 Nov 12 '15 at 12:32
  • @Squazz Modified my answer. Now you can pass the URL as a parameter. – MichaelS Nov 12 '15 at 12:44
  • trying to do this, all that happens is notepad opening the file? – Squazz Nov 12 '15 at 12:47
  • Wait what? What is your file called? Why is notepad opening ps1 files? Is it possible that your file is called something like file.ps1.txt? – MichaelS Nov 12 '15 at 12:49
  • Nope, the filetype is just .ps1, but I can see in the preferences that it is set to be opened with Notepad :/ Never touched any of this, never run powerscript on my conputer before, so don't know why that is – Squazz Nov 12 '15 at 12:51
  • Do you even have powershell installed? What happens if oyu execute powershell.exe? – MichaelS Nov 12 '15 at 12:55
  • If it is, try to execute this code: `powershell "C:\MyScript.ps1 -url http://www.google.com"` (insert proper path to the script and the URL ^^) This should output the HTML code of the destination URL to the console. Just ignore the output. It just makes sure that your script worked. – MichaelS Nov 12 '15 at 12:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94927/discussion-between-michaels-and-squazz). – MichaelS Nov 12 '15 at 13:00
  • 1
    Haha, I was just about to click "submit" to the exactly same edit :P – Squazz Nov 12 '15 at 13:11
  • Now, I'm not strong in powershell, but isn't this a GET request? Would love to be able to lock it down so only POST requests are accepted by my action. Is there a way to make the script do a POST request? – Squazz Nov 12 '15 at 13:15
  • 1
    Not sure how exactly to do this but this post should answer your question http://stackoverflow.com/questions/17325293/invoke-webrequest-post-with-parameters – MichaelS Nov 12 '15 at 13:17