.net 在 Windows 上通过计划任务加载 URL 的推荐方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1987345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:43:33  来源:igfitidea点击:

Recommended method for loading a URL via a scheduled task on Windows

.netwindowsinternet-explorerschedulingscheduled-tasks

提问by Cory House

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:

我在 Windows 机器上托管了一个网页,我需要确保每天至少加载一次。我目前的计划是创建一个计划任务,打开 Internet Explorer 并点击 URL:

"C:\Program Files\Internet Explorer\iexplore.exe" myurl.com/script_to_run_daily.aspx

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.

这很容易设置并且工作正常,但它让我觉得是一个黑客,因为 Internet Explorer 实际上必须打开并点击这个 URL。我不需要从这个页面返回任何输入,它只是在命中时将缓存的数据存储在文件中。

Is there a slicker way of doing this? In case it matters, this is a VB.net site.

有没有更巧妙的方法来做到这一点?如果重要,这是一个 VB.net 站点。

Thanks in advance!

提前致谢!

回答by Nikhil Dabas

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:

正如 Remus Rusanu 指出的那样,PowerShell 将是最佳选择。这是一个简单的单行代码,您可以使用它来创建计划任务,而无需编写单独的 .ps1 文件:

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

The above will work for PowerShell 3+. If you need something that will work with older versions, here's the one-liner:

以上将适用于 PowerShell 3+。如果您需要可以与旧版本一起使用的东西,这里是单行:

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

The schtasksexamples set up the task to run daily - consult the schtasks documentationfor more options.

这些schtasks示例将任务设置为每天运行 -有关更多选项,请参阅schtasks 文档

回答by Remus Rusanu

You can schedule a PowerShellscript. PS is pretty powerfull and gives you access to the entire .Net Framework, plus change. Here is an example:

您可以安排PowerShell脚本。PS 非常强大,可让您访问整个 .Net 框架,并进行更改。下面是一个例子:

$request = [System.Net.WebRequest]::Create("http://www.example.com")
$response = $request.GetResponse()
$response.Close()

回答by RickNZ

Another option is VB Script. For example (save as file.vbs):

另一种选择是 VB 脚本。例如(另存为 file.vbs):

sSrcUrl = "http://yourdomain.com/yourfile.aspx"
sDestFolder = "C:\yourfolder\"
sImageFile = "filename.txt"
set oHTTP = WScript.CreateObject("MSXML2.ServerXMLHTTP")
oHTTP.open "GET", sSrcUrl, False
oHTTP.send ""
set oStream = createobject("adodb.stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
oStream.type = adTypeBinary
oStream.open
oStream.write oHTTP.responseBody
oStream.savetofile sDestFolder & sImageFile, adSaveCreateOverWrite
set oStream = nothing
set oHTTP = nothing
WScript.Echo "Done..."

回答by Johnny Oshika

As of PowerShell 5.0, curlis an alias for Invoke-WebRequest, so you can create a Scheduled Task as follows:

从 PowerShell 5.0 开始,curl是 的别名Invoke-WebRequest,因此您可以按如下方式创建计划任务:

Action: Start a program
Program/script: powershell
Add arguments: curl http://www.example.com/foo/bar

You can also use Invoke-WebRequest, but curlis more concise.

您也可以使用Invoke-WebRequest,但curl更简洁。

回答by Todd

This SuperUser.com answeris much simpler.

这个 SuperUser.com 答案要简单得多。

cmd /c start http://example.com/somePage.html

More specifically, here's how it looks in a Windows Scheduled Task:

更具体地说,这是它在 Windows 计划任务中的外观:

enter image description here

在此处输入图片说明

Although, this likely runs the default configured browser in the background. If it must be IE, then that should be configured. This is likely the best option. It's good to have a solution which runs directly as an Action step of the Task. All the information is right there. Even better if it's terse, so you can see most of the command without scrolling across the textbox.

虽然,这可能会在后台运行默认配置的浏览器。如果必须是 IE,则应进行配置。这可能是最好的选择。最好有一个直接作为任务的操作步骤运行的解决方案。所有信息都在那里。如果它是简洁的,那就更好了,这样您就可以在不滚动文本框的情况下看到大部分命令。

WGET / CURL - good alternative

WGET / CURL - 不错的选择

If anything, WGET would be a good alternative to cmd..start..url. I'm not the first to think of this. The downside is you have to download WGET for Windows for it to work - it isn't an out-of-the-box solution. But WGET is lighter and importantly gives you more power. You can send lots of other types of URL calls. This includes different HTTP methods such as POST, custom Headers and more. Curl is a good option, instead of Wget - they have different sets of features, many overlapping.

如果有的话,WGET 将是cmd..start..url. 我不是第一个想到这一点的人。缺点是您必须为 Windows 下载 WGET 才能工作 - 它不是开箱即用的解决方案。但 WGET 更轻,重要的是给你更多的力量。您可以发送许多其他类型的 URL 调用。这包括不同的 HTTP 方法,例如 POST、自定义标头等。Curl 是一个不错的选择,而不是 Wget - 它们具有不同的功能集,许多重叠。

PowerShell - not recommended

PowerShell - 不推荐

PowerShell, might be considered a "lighter" option, but you're also loading up a PowerShell (.Net AppDomain) which has a bit of lag, and also the command is a lot longer, and therefore harder to remember if you're doing this regularly. So PowerShell isn't the best option.

PowerShell,可能被认为是一个“更轻”的选项,但你也正在加载一个有一点滞后的 PowerShell (.Net AppDomain),而且命令更长,因此如果你是这样的话就更难记住定期这样做。所以 PowerShell 不是最好的选择。

VBScript - not recommended

VBScript - 不推荐

Clearly, VBScript is less recommendable. You have so much more you would need to remember, and the script is quite large. Unless you have other steps which warrant a script, don't have a completely separate resource just to do something you could have accomplished directly within a Scheduled Task.

显然,VBScript 不太值得推荐。您需要记住的还有很多,而且脚本非常大。除非您有其他需要脚本的步骤,否则不要使用完全独立的资源来执行您可以直接在计划任务中完成的事情。

Also, see similar questions:

另外,请参阅类似的问题:

回答by Jacob Mattison

There are Windows versions of the most common command-line http request tools, such as cURLand wget. You could certainly create a scheduled task that would run one of these. I have also done this from within a Windows Scripting Host script, if you needed to loop or create URL parameters on the fly, or some such.

有最常见的命令行 http 请求工具的 Windows 版本,例如cURLwget。您当然可以创建一个计划任务来运行其中之一。如果您需要动态循环或创建 URL 参数,我也在 Windows Scripting Host 脚本中完成了此操作。

回答by Johny papa

tried with curl on PowerShell 4.0, curl is an alias for Invoke-WebRequest, so you can create a Scheduled Task as follows:

在 PowerShell 4.0 上尝试使用 curl,curl 是 Invoke-WebRequest 的别名,因此您可以按如下方式创建计划任务:

Action: Start a program Program/script: powershell Add arguments: curl http://www.example.com/foo/bar

操作:启动一个程序程序/脚本:powershell 添加参数:curl http://www.example.com/foo/bar

worked like a charm!

像魅力一样工作!