How To Script Downloads With BITS? #Batch

How To Script Downloads With BITS? #Batch

Tags :

Category : Batch , Tool

When it comes to large file downloads one should use a download manager which at least is able to resume broken downloads. Windows has a built-in download manager service called Background Intelligent Transfer Service(BITS). Some of us at Login Consultants (including me) prefer to script large file downloads with the Microsoft’s BITS Admin Utility – Bitsadmin. This article explains how to use Bitsadmin to download a file.

The BITS service is built into Windows XP, Vista, Server 2003, and Server 2008. For example the Windows Automatic Update, WSUS 3.0, or Microsoft SCCM 2007 depend on BITS to transfer files like Windows Updates and software packages. For one thing BITS has been designed for robustness over slow or unstable network connections, and for another thing BITS is a background process that economizes on bandwidth usage. If a download does not complete due to a network problem or restart of your machine, BITS will automatically try to continue the download from where it left off, and repeat this until the whole file has been retrieved. BITS file transfers can be invoked and managed from the command line with the Bitsadmin utility which is part of the Windows Support Tools.

Please note that this article applies to Bitsadmin 3.0 and the version of BITS that is included in Windows Vista SP1.

In order to download a file with Bitsadmin, respectively BITS, you need to type a series of Bitsadmin commands to create a download job, to add the file the the job, eventually to set credentials and proxy settings, and finally to start the download job. Bitsadmin has many command line switches, for this see also Bitsadmin’s help by typing “bitsadmin” at the command prompt. For our purposes we need the switches CREATE, ADDFILE, SETNOTIFYCMDLINE, COMPLETE, SETCREDENTIALS (maybe), and RESUME.

Let’s say we want to download the english copy of the Automated Installation Kit (AIK) for Windows Vista SP1 and Windows Server 2008. This is a real large download, the ISO file is about 1350 MB. Since the URL string is very large the examples below don’t contain the complete download URL. If you want to reproduce the examples you must use this URL:

1. Create Job

First of all, we need to create a new download job with Bitsadmin /CREATE

Bitsadmin will confirm job creation by echoing the job GUID like “Created job {58C6770A-AB4E-477C-8944-33246EC8791E}”. For now, the job is created and its state is suspended. (Therefore, we will need to resume the job later in order to start the download process.)

2. Add File

Now we can add the file to the job with Bitsadmin /ADDFILE. We need to specify the job name, the download URL, and the file’s local name.

Again, Bitsadmin will confirm the action. (Hint: if you want to add multiple files to a job take a look at Bitsadmin’s switch /ADDFILESET which reads each file’s remote URL and local name from a text file.)

3. Optional Settings

The job is still in suspended state, and we are almost ready to start the download. But to start with we should pay attention to three important and useful Bitsadmin switches: COMPLETE, SETNOTIFYCMDLINE, and SETCREDENTIALS

By default, Bitsadmin doesn’t tell anything about a finished file transfer. Even worse, though BITS may have downloaded a file completely you won’t find it unless you have completed the job explicitly. To kill two birds with one stone we will use Bitsadmin /SETNOTIFYCMDLINE to launch Bitsadmin /COMPLETE as loon as the transfer is finished. (Virtually, SETNOTIFYCMDLINE enables you to set a command line to execute for notification when the file download is finished.)

Maybe the file is located on a password protected web server. In this case you can add credentials to a job using Bitsadmin /SETCREDENTIALS. This time you have to go without an example. Please refer to Bitsadmin’s syntax help by typing “bitsadmin” at the command line.

4. Start/Resume Download

Finally, we are ready to resume the download job with Bitsadmin /RESUME:

Now, BITS is downloading the file in the background. Since we have defined the launch of an according job completion command on transfer completion we can just wait and see… or check out some Bitsadmin command lines:

5. Monitor or Cancel Job

How to display information about the BITS job?

How to retrieve the size of the BITS job?

How to retrieve the number of bytes transferred?

How to monitor the BITS copy manager?

How to cancel the BITS job?

How to delete all BITS jobs?

6. Write a Shell Script

The batch file below, Bits-Download.cmd, simplifies the usage of Bitsadmin: