Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could a windows scheduled task connect to a rest endpoint?

I have a rest service written in ASP.NET Web API.

I want a scheduled task to connect to an endpoint like:

www.example.com/jobs/job1

I want to be able to set the interval time to say every 12 hours.

Is it possible to do this with a scheduled task?

I want to avoid having to create a windows service just to ping a rest endpoint.

like image 577
public static Avatar asked Aug 21 '14 20:08

public static


2 Answers

You can easily accomplish this with PowerShell and System.Net.WebClient.

Create a simple MyScriptName.ps1 file with the following contents:

$web = New-Object System.Net.WebClient
$str = $web.DownloadString("http://www.example.com/jobs/job1")
$str # not strictly necessary but if you run this in PowerShell you will get the response body of your endpoint

Then create a new scheduled task and add a new action to Start a program and use the following settings:

Program/script: powershell
Add arguments: .\MyScriptName.ps1
Start in: C:\The\Directory\You\Saved\Your\Script\In
like image 98
Sean Airey Avatar answered Sep 30 '22 11:09

Sean Airey


Starting with PowerShell 3.0 you can use the Invoke-RestMethod cmdlet.

Invoke-RestMethod -Uri "www.example.com/jobs/job1"

The advantage here is that it will deserialize it for you into an object if it's XML or JSON. For RSS or ATOM it will return the Item or Entry XML nodes. For text it will display the text.

You can read more here: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/invoke-restmethod

Create the scheduled task with the details below:

Trigger: Daily, at your chosen time, repeat 12 hours for a duration of 1 day.

Actions:

Start a program: powershell

Arguments: -NoProfile -NonInteractive -File ".\YourScriptName.ps1"

Start in: C:\your_scripts

Given that you have created an object you can format this data any way you choose but that's beyond the scope of this question.

like image 30
Matt R Avatar answered Sep 30 '22 12:09

Matt R