Recommended method for loading a URL via a scheduled task on Windows
Asked Answered
F

8

65

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.

Firecure answered 31/12, 2009 at 22:33 Comment(1)
Whilst a proper VB script is a better way to go you may wish to note that it is possible to stop the task if it runs for longer than an hour in the settings tab of Task Scheduler.Corroboration
P
128

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.

Purify answered 22/8, 2012 at 21:29 Comment(7)
Do you have an example that includes a querystring (and escaping of an ampsersand?)Puny
@PaulGeorge replacing ampersands with ^& should do the trick.Purify
This works for me, single quotes, no change to ampersands : powershell -ExecutionPolicy unrestricted -Command "(New-Object Net.WebClient).DownloadString('localhost/cron.aspx?var1=cat&var2=dog')"Puny
Is there a way to add something to this one-liner to include ignoring SSL warnings?Implicate
Using curl is another option since Powershell 4.0Girgenti
How can i add bunch of urls in a single task?Collusion
Thanks for this answer. The -UseBasicParsing parameter is very useful. It avoids getting an error of website being blocked by Internet Explorer Enhanced Security ConfigurationLuckey
S
42

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()
Supervisory answered 31/12, 2009 at 23:14 Comment(0)
B
18

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..."
Bonanza answered 31/12, 2009 at 23:36 Comment(2)
Ah, I should've thought of VBScript. Great idea. A +1 and an accept for you. Others had great ideas too, but you did the hard work of presenting a complete solution! Many thanks Rick!Firecure
how will i do this in ubuntu server? alternative for vbs? java?Renferd
K
16

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:

Koonce answered 10/9, 2016 at 12:31 Comment(5)
Hi @Todd, this solution will open a browser not run at backgroundBullough
Background operation wasn't a requirement of the op. Although that's easy enough to do with cmd, I would think, in combination with these options.Koonce
I tried the Powershell solutions first offered by Nikhil Dabas and Remus Rusanu, but neither worked. This is the one that is working well for my needs.Gunlock
can we call two webpages by single task scheduler? Basically I need to call 3 webpages and i don't want to create 3 diff schedulers for it.Centralize
You might be able to combine multiple statements with the cmd method (untested): /c start https://url1.com & start https://url2.com. see #8055871Koonce
T
12

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.

Thevenot answered 18/10, 2016 at 20:11 Comment(1)
As always, the correct answer is always last. Using cmd /c will open a browser window, which you don't necessarily wantPearly
A
5

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.

Antrum answered 31/12, 2009 at 22:41 Comment(1)
+1 for many great options, I especially like the Windows Scripting Host idea, but went w/ Rick's solution since he went the extra mile w/ the complete solution. Thanks for the ideas for the future though!Firecure
A
1

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!

Astatine answered 4/7, 2018 at 8:59 Comment(1)
Welcome to Stack Overflow! While links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: How do I write a good answer?Illation
A
1

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.

Airspeed answered 28/1, 2021 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.