Can a scheduled Windows task connect to a resting endpoint?

I have a leisure service written in the ASP.NET Web API.

I want the scheduled task to be connected to the endpoint, for example:

www.example.com/jobs/job1 

I want the interval time to be indicated every 12 hours.

Is it possible to do this with a planned task?

I want there to be no need to create a Windows service to ping the resting endpoint.

+6
source share
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 
+13
source

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

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

The advantage is that it will deserialize it for you into an object if it is XML or JSON. For RSS or ATOM, it will return Item or Entry XML objects. For text, it will display text.

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

Create a scheduled task with the following details:

Trigger: daily, at the time you specify, repeat 12 hours for 1 day.

Actions:

Run the program: powershell

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

Start: C: \ your_scripts

Given that you created the object, you can format this data in any way that you choose, but which is beyond the scope of this question.

0
source

Source: https://habr.com/ru/post/974181/


All Articles