如何在BITS下载完成时运行脚本 [英] How to run script on BITS download completion

查看:214
本文介绍了如何在BITS下载完成时运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动下载和安装大型应用程序,该应用程序大小从几百MB到几GB.我正在研究使用BITS和Powershell异步下载应用程序,然后启动安装程序.

使用不推荐使用的bitsadmin命令,有一个/SETNOTIFYCMDLINE选项,一旦下载完成,该选项将允许我链接设置的执行.如何使用Powershell执行此操作?

这将是我的第一个powershell脚本,因此,如果您有任何指向示例的链接都很好.谢谢

我建议使用BitsTransfer模块,因为它公开了用于处理BITS作业的本机PowerShell方法.首先,只需指示PowerShell加载BITS模块:

Import-Module BitsTransfer

运行 Get-Command 以查看添加了哪些新的BITS cmdlet,该操作显示:/p>

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

您最有可能感兴趣的是 Start-BitsTransfer :

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

该cmdlet将在屏幕上显示进度条,并等待下载完成-脚本中的下一个命令将在下载完成后才会执行.

对于异步任务,可以将-Asynchronous参数添加到Start-BitsTransfer cmdlet,它将使下载排队,并使其在后台运行.您可以使用 Get-BitsTransfer

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

解决方案

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

这篇关于如何在BITS下载完成时运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆