I have a rest service written in ASP.NET Web API.
我有一个用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.
我希望能够设置每隔12小时说一次的间隔时间。
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.
我想避免创建一个Windows服务只是为了ping一个休息端点。
2 个解决方案
#1
14
You can easily accomplish this with PowerShell and System.Net.WebClient
.
您可以使用PowerShell和System.Net.WebClient轻松完成此任务。
Create a simple MyScriptName.ps1
file with the following contents:
使用以下内容创建一个简单的MyScriptName.ps1文件:
$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
#2
0
Starting with PowerShell 3.0 you can use the Invoke-RestMethod cmdlet.
从PowerShell 3.0开始,您可以使用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.
这里的优点是,如果它是XML或JSON,它会将它反序列化为一个对象。对于RSS或ATOM,它将返回Item或Entry XML节点。对于文本,它将显示文本。
You can read more here: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/invoke-restmethod
你可以在这里阅读更多内容: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.
触发:每天,在您选择的时间,重复12小时,持续1天。
Actions:
操作:
Start a program: powershell
启动程序:powershell
Arguments: -NoProfile -NoInteractive -File ".\YourScriptName.ps1"
参数:-NoProfile -NoInteractive -File“。\ YourScriptName.ps1”
Start in: C:\your_scripts
开始于: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.
鉴于您已创建了一个对象,您可以按照您选择的方式格式化此数据,但这超出了此问题的范围。
#1
14
You can easily accomplish this with PowerShell and System.Net.WebClient
.
您可以使用PowerShell和System.Net.WebClient轻松完成此任务。
Create a simple MyScriptName.ps1
file with the following contents:
使用以下内容创建一个简单的MyScriptName.ps1文件:
$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
#2
0
Starting with PowerShell 3.0 you can use the Invoke-RestMethod cmdlet.
从PowerShell 3.0开始,您可以使用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.
这里的优点是,如果它是XML或JSON,它会将它反序列化为一个对象。对于RSS或ATOM,它将返回Item或Entry XML节点。对于文本,它将显示文本。
You can read more here: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/invoke-restmethod
你可以在这里阅读更多内容: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.
触发:每天,在您选择的时间,重复12小时,持续1天。
Actions:
操作:
Start a program: powershell
启动程序:powershell
Arguments: -NoProfile -NoInteractive -File ".\YourScriptName.ps1"
参数:-NoProfile -NoInteractive -File“。\ YourScriptName.ps1”
Start in: C:\your_scripts
开始于: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.
鉴于您已创建了一个对象,您可以按照您选择的方式格式化此数据,但这超出了此问题的范围。