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

发布于 2024-08-16 04:08:56 字数 349 浏览 3 评论 0原文

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

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

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

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

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 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怪异←思 2024-08-23 04:08:56

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

powershell -ExecutionPolicy Bypass -Command
    Invoke-WebRequest 'http://localhost/cron.aspx' -UseBasicParsing

请注意,在所有这些命令行中添加换行符只是为了清楚起见。在尝试运行命令之前删除它们。

您可以像这样创建计划任务:(从提升的命令提示符运行此命令)

schtasks /create /tn "MyAppDailyUpdate"
    /tr "powershell -ExecutionPolicy Bypass -Command
        Invoke-WebRequest 'http://localhost/cron.aspx' -UseBasicParsing"
    /sc DAILY /ru System

上面的内容适用于 PowerShell 3+。如果您需要一些可以与旧版本一起使用的东西,这里有一句话:

powershell -ExecutionPolicy unrestricted -Command
    "(New-Object Net.WebClient).DownloadString(\"http://localhost/cron.aspx\")"

您可以像这样创建计划任务:(从提升的命令提示符)

schtasks /create /tn "MyAppDailyUpdate"
    /tr "powershell -ExecutionPolicy unrestricted -Command
        \"(New-Object Net.WebClient).DownloadString(\\\"http://localhost/cron.aspx\\\")\""
    /sc DAILY /ru System

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

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

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

The schtasks examples set up the task to run daily - consult the schtasks documentation for more options.

七堇年 2024-08-23 04:08:56

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

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

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

$request = [System.Net.WebRequest]::Create("http://www.example.com")
$response = $request.GetResponse()
$response.Close()
ぃ弥猫深巷。 2024-08-23 04:08:56

另一种选择是 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..."

Another option is VB Script. For example (save as 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..."
却一份温柔 2024-08-23 04:08:56

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

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

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

在此处输入图像描述

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

WGET / CURL - 不错的替代方案

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

PowerShell - 不推荐

PowerShell,可能被认为是“更轻量级”的选项,但您还加载了一个有点滞后的 PowerShell (.Net AppDomain),而且命令也很多时间更长,因此如果你经常这样做就更难记住。因此 PowerShell 并不是最佳选择。

VBScript - 不推荐

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

另请参阅类似的问题:

This SuperUser.com answer is much simpler.

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

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

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.

WGET / CURL - good alternative

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.

PowerShell - not recommended

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.

VBScript - not recommended

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.

Also, see similar questions:

◇流星雨 2024-08-23 04:08:56

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

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

您还可以使用 Invoke-WebRequest code>,但 curl 更简洁。

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

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

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

作死小能手 2024-08-23 04:08:56

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

There are Windows versions of the most common command-line http request tools, such as cURL and 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.

回忆凄美了谁 2024-08-23 04:08:56

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

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

就像魅力!

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

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

worked like a charm!

任谁 2024-08-23 04:08:56

如果您在运行 PowerShell 脚本时收到“底层连接已关闭:发送时发生意外错误”消息,请将以下代码行添加到脚本顶部:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

使用 TLS 1.2 的最新支持并弃用 TLS 1.0、1.1 等,您的 PowerShell 脚本需要进行必要的更改。

If you are getting "The underlying connection was closed: An unexpected error occurred on a send" message while running your PowerShell script then add the following line of code to the top of your script:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

With the latest support of TLS 1.2 and deprecation of TLS 1.0, 1.1 etc., your PowerShell scripts need this necessary change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文