3

I am trying to automate the download and installation of a large application that is several hundreds of MB to a few GB in size. I am looking into using BITS and powershell to asynchronously download the application and then launch the setup.

Using the deprecated bitsadmin command there is a /SETNOTIFYCMDLINEoption that would allow me to chain the execution of the setup once the download completes. How can I perform this with powershell?

This will be my first powershell script, so if you have any links to examples that would be great. Thanks

Stephen Nutt
  • 3,258
  • 1
  • 21
  • 21

1 Answers1

4

I would suggest using the BitsTransfer module as it exposes native PowerShell methods for working with BITS jobs. To get started, you simply instruct PowerShell to load the BITS module:

Import-Module BitsTransfer

Running Get-Command to see what new BITS cmdlets have been added shows:

PS C:\> Get-Command  *-bits*

CommandType     Name
-----------     ----
Cmdlet          Add-BitsFile
Cmdlet          Complete-BitsTransfer
Cmdlet          Get-BitsTransfer
Cmdlet          Remove-BitsTransfer
Cmdlet          Resume-BitsTransfer
Cmdlet          Set-BitsTransfer
Cmdlet          Start-BitsTransfer
Cmdlet          Suspend-BitsTransfer

The one you will most likely be interested in would be Start-BitsTransfer:

Start-BitsTransfer -Source http://localhost/BigInstaller.msi

The cmdlet will show a progress bar on the screen and wait for the download to finish - the next command in your script won't execute until the download has finished.

For async tasks, you can add the -Asynchronous parameter to the Start-BitsTransfer cmdlet, which will queue up the download and let it run in the background. You can manage those downloads with the Get-BitsTransfer and Complete-BitsTransfer cmdlets.

PS C:\> Start-BitsTransfer -Source http://localhost/BigInstaller.msi -Async
JobId                   DisplayName    TransferType  JobState
-----                   -----------    ------------  --------
da7bab7f-fbfd-432d-8... BITS Transfer  Download      Connecting

PS C:\> Get-BitsTransfer
JobId                   DisplayName    TransferType  JobState
-----                   -----------    ------------  --------
da7bab7f-fbfd-432d-8... BITS Transfer  Download      Transferred

# finish and jobs that have transferred (e.g. write them to destination on disk)
PS C:\> Get-BitsTransfer | ? {$_.JobState -eq "Transferred"} | Complete-BitsTransfer
Goyuix
  • 23,614
  • 14
  • 84
  • 128
  • I'd like the download to be asynchronous, so allowing the user to log out and when they log back in the download continue. Given this, the script will not be running. – Stephen Nutt Jul 20 '12 at 20:08
  • Is there a way to be notified that my download is complete when I use the -Asynchronous parameter? My script that initiates the download is nor working (thank you for the examples) so I am now left trying to launch my completion script. – Stephen Nutt Jul 20 '12 at 22:57
  • If you want to execute something on completion, why not just let it run synchronously? You can always launch another copy of PowerShell if you need to do additional scripting tasks... also note that the -Source and -Destination parameters take string arrays - so you can queue up multiple files with ease if you need to. – Goyuix Jul 21 '12 at 20:32
  • I do not want to run it synchronously as the download may take more time than our customers are on-line for a single session. I would like to use the BITS ability for a notification when the download is complete. – Stephen Nutt Jul 23 '12 at 12:57
  • 1
    It would appear as though the notification options are quite limited with the BitsTransfer module. It is worth noting that any transfers that are started asynchronously will continue transferring in the background even if the user exits PowerShell. The best you will do while staying native to the BitsTransfer module would be to periodically poll the job status (at login, after it is queued, etc.) and take action when it is completed. – Goyuix Jul 28 '12 at 22:14
  • Thanks for your help, especially for your detailed examples. – Stephen Nutt Jul 30 '12 at 12:49