开发者

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

开发者_高级运维

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.

Thanks in advance!


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.


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()


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


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:

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

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:

  • https://superuser.com/questions/321215/how-can-i-open-a-url-on-a-schedule-in-the-default-browser
  • Simple-as-possible task schedule to call web page


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.


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.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜