通过批处理文件创建计划任务 "ping"/"poke" a URL
Creating a scheduled task to "ping"/"poke" a URL via a batch file
我想做什么
我想每天 ping/poke 一个 URL 来激活我用 C# 编写的一个动作,并且 运行正在网络服务器上。为此,我希望我可以使用任务调度程序。
我的设置
我已经设置了调用此程序的计划任务操作:
C:\...\_resources\callurl.cmd"
根据取自 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);
然后我将以下参数添加到任务中
/c "http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
使用“/c”的想法来自 this question。
用于运行任务的用户帐户是SYSTEM
当我这样做时没有任何反应。有时会弹出 i CMD windows 一瞬间,有时什么也没有。在这两种情况下,我都看不到我的操作被命中(调试应用程序并在入口点设置断点)
运行 它在 CMD
如果我打开我的命令提示符,运行 像这样的命令
callurl.cmd "http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
没问题。这里 callurl.cmd 做了我期望它做的事情,它调用了我的操作。
我做错了什么?这里有什么我没看到的吗?
我强烈建议改为通过 Powershell 执行此操作。它更容易,并且可以与调度程序完美配合。这是 call/trigger a URL:
的代码
$url="http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
(New-Object System.Net.WebClient).DownloadString("$url");
将此代码保存在 *.ps1 文件中并创建一个任务来执行它。而已。没有批处理,没有 Java-/VBScript,等等。只有纯 Powershell。
如果您不允许在您的计算机上执行 ps 脚本,但您可以这样做:
- 以管理员身份打开 Powershell
- 执行命令
set-executionpolicy remotesigned
有不同的可行政策而不是 remotesigned
。我什至更喜欢 unrestricted
。
编辑: 如果你希望能够传递一个 URL 作为参数,这里是修改后的代码:
param([String]$url="")
(New-Object System.Net.WebClient).DownloadString("$url");
使用filename.ps1 -url http://localhost/Controller/ActionToInvoke?guid=SOME-GUID
调用文件。
您也可以转到任务计划程序,创建一个新任务,输入所有随机内容,转到 "Actions" 在 "Program/script" 字段中输入 powershell
,然后 "C:\MyScript.ps1 -url http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
进入 "Add arguments (optional)" 字段。
我想做什么
我想每天 ping/poke 一个 URL 来激活我用 C# 编写的一个动作,并且 运行正在网络服务器上。为此,我希望我可以使用任务调度程序。
我的设置
我已经设置了调用此程序的计划任务操作:
C:\...\_resources\callurl.cmd"
根据取自 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);
然后我将以下参数添加到任务中
/c "http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
使用“/c”的想法来自 this question。
用于运行任务的用户帐户是SYSTEM
当我这样做时没有任何反应。有时会弹出 i CMD windows 一瞬间,有时什么也没有。在这两种情况下,我都看不到我的操作被命中(调试应用程序并在入口点设置断点)
运行 它在 CMD
如果我打开我的命令提示符,运行 像这样的命令
callurl.cmd "http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
没问题。这里 callurl.cmd 做了我期望它做的事情,它调用了我的操作。
我做错了什么?这里有什么我没看到的吗?
我强烈建议改为通过 Powershell 执行此操作。它更容易,并且可以与调度程序完美配合。这是 call/trigger a URL:
的代码$url="http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
(New-Object System.Net.WebClient).DownloadString("$url");
将此代码保存在 *.ps1 文件中并创建一个任务来执行它。而已。没有批处理,没有 Java-/VBScript,等等。只有纯 Powershell。
如果您不允许在您的计算机上执行 ps 脚本,但您可以这样做:
- 以管理员身份打开 Powershell
- 执行命令
set-executionpolicy remotesigned
有不同的可行政策而不是 remotesigned
。我什至更喜欢 unrestricted
。
编辑: 如果你希望能够传递一个 URL 作为参数,这里是修改后的代码:
param([String]$url="")
(New-Object System.Net.WebClient).DownloadString("$url");
使用filename.ps1 -url http://localhost/Controller/ActionToInvoke?guid=SOME-GUID
调用文件。
您也可以转到任务计划程序,创建一个新任务,输入所有随机内容,转到 "Actions" 在 "Program/script" 字段中输入 powershell
,然后 "C:\MyScript.ps1 -url http://localhost/Controller/ActionToInvoke?guid=SOME-GUID"
进入 "Add arguments (optional)" 字段。