在 Windows 上通过计划任务加载 URL 的推荐方法 [英] Recommended method for loading a URL via a scheduled task on Windows
问题描述
我在 Windows 机器上托管了一个网页,我需要确保每天至少加载一次.我目前的计划是创建一个计划任务,打开 Internet Explorer 并点击 URL:
I have a webpage hosted on a Windows box that I need to assure gets loaded at least once/day. My current plan is to create a scheduled task that opens Internet Explorer and hits the URL:
"C:Program FilesInternet Exploreriexplore.exe" myurl.com/script_to_run_daily.aspx
这很容易设置并且工作正常,但它让我觉得是一个黑客,因为 Internet Explorer 实际上必须打开并点击这个 URL.我不需要从这个页面返回任何输入,它只是在命中时将缓存数据存储在文件中.
This was simple to setup and works fine, but it strikes me as a hack because Internet Explorer actually has to open and hit this URL. I don't need any input back from this page, it simply stores cached data in files when it's hit.
有没有更巧妙的方法来做到这一点?以防万一,这是一个 VB.net 站点.
Is there a slicker way of doing this? In case it matters, this is a VB.net site.
提前致谢!
推荐答案
正如 Remus Rusanu 所指出的,PowerShell 将是可行的方法.这是一个简单的单行代码,您可以使用它来创建计划任务,而无需编写单独的 .ps1 文件:
As pointed out by Remus Rusanu, PowerShell would be the way to go. Here's a simple one-liner that you can use to create a scheduled task, without needing to write a separate .ps1 file:
powershell -ExecutionPolicy Bypass -Command
Invoke-WebRequest 'http://localhost/cron.aspx' -UseBasicParsing
请注意,在所有这些命令行中添加换行符只是为了清楚起见.在尝试运行命令之前删除它们.
Note that line breaks are added only for clarity in all of these command lines. Remove them before you try running the commands.
您可以像这样创建计划任务:(从提升的命令提示符运行)
You can create the scheduled task like this: (run this from an elevated command prompt)
schtasks /create /tn "MyAppDailyUpdate"
/tr "powershell -ExecutionPolicy Bypass -Command
Invoke-WebRequest 'http://localhost/cron.aspx' -UseBasicParsing"
/sc DAILY /ru System
以上适用于 PowerShell 3+.如果您需要可以与旧版本一起使用的东西,这里是单行:
The above will work for PowerShell 3+. If you need something that will work with older versions, here's the one-liner:
powershell -ExecutionPolicy unrestricted -Command
"(New-Object Net.WebClient).DownloadString("http://localhost/cron.aspx")"
您可以像这样创建计划任务:(从提升的命令提示符)
You can create the scheduled task like this: (from an elevated command prompt)
schtasks /create /tn "MyAppDailyUpdate"
/tr "powershell -ExecutionPolicy unrestricted -Command
"(New-Object Net.WebClient).DownloadString(\"http://localhost/cron.aspx\")""
/sc DAILY /ru System
schtasks
示例将任务设置为每天运行 - 请参阅 schtasks 文档 了解更多选项.
The schtasks
examples set up the task to run daily - consult the schtasks documentation for more options.
这篇关于在 Windows 上通过计划任务加载 URL 的推荐方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!