共用方式為


Configuring BITS Upload Server with PowerShell and ADSI

A common request that I get from customers is that they would like to use PowerShell to configure the BITS Upload Server extension on IIS. In the past, automating upload server configuration required the use of the Windows Scripting Host and the BITS ADSI extension (see https://msdn.microsoft.com/en-us/library/aa362825(v=VS.85).aspx ). It is also possible to do this using PowerShell and the same BITS ADSI extension.

First, let’s begin by installing the BITS Upload Server extension for IIS. We will need to import the Server Manager cmdlets and use the Add-WindowsFeature cmdlet to do this.

PS> Import-Module ServerManager

PS> Add-WindowsFeature BITS-IIS-Ext

In order to enable or disable BITS uploads using the BITS ADSI extension; we will need to run PowerShell using Single-Threaded Apartment (STA).

PS> powershell.exe –sta

Let’s start configuring a website to allow BITS uploads. To do this, we will need an ADSI object that represents the website’s configuration settings. In the following example, we get the configuration for an IIS directory called “Uploads” under the “Default Web Site”.

PS> $siteObj = New-Object System.DirectoryServices.DirectoryEntry("IIS://LocalHost/W3SVC/1/root/Uploads")

To enable BITS uploads on this directory; we can call the EnableBitsUploads method. (Note: The local path associated with this directory must allow write access to the built-in “IUSR” user. Otherwise, any BITS uploads to this directory will result in an access denied error.)

PS> $siteObj.EnableBitsUploads()

We call the RefreshCache method to synchronize the ADSI object with the latest configuration settings. We can then get a list of the configurable properties for this directory. The properties that affect the BITS upload server configuration are highlighted below. You can find out more about them at https://msdn.microsoft.com/en-us/library/aa362818(v=VS.85).aspx .

PS > $siteObj.RefreshCache()

PS > $siteObj.Properties

PropertyName Value Capacity Count

------------ ----- -------- -----

AppPoolId DefaultAppPool 4 1

DontLog False 4 1

DirBrowseFlags 1073741854 4 1

DefaultDoc Default.htm,Default.asp,in... 4 1

AuthFlags 1 4 1

AnonymousUserName IUSR 4 1

AspDiskTemplateCacheDirectory D:\inetpub\temp\ASP Compil... 4 1

HttpErrors {401,*,FILE,D:\inetpub\cus... 9 9

AccessSSLFlags 0 4 1

NTAuthenticationProviders Negotiate,NTLM 4 1

AccessFlags 513 4 1

ScriptMaps {.asp,D:\Windows\system32\... 14 14

RedirectHeaders 0 0

HttpCustomHeaders X-Powered-By: ASP.NET 4 1

BITSSessionDirectory BITS-Sessions 4 1

BITSMaximumUploadSize 18446744073709551615 4 1

BITSSessionTimeout 1209600 4 1

BITSServerNotificationType 0 4 1

BITSServerNotificationURL 4 1

BITSHostId 4 1

BITSHostIdFallbackTimeout 86400 4 1

BITSAllowOverwrites 0 4 1

BITSCleanupUseDefault True 4 1

BITSCleanupCount 12 4 1

BITSCleanupUnits 1 4 1

BITSMaxUploadSizeUnits 0 4 1

BITSSessionTimeoutUnits 0 4 1

BITSHostIdFallbackTimeoutU... 0 4 1

BITSNumberOfSessionsLimit 50 4 1

BITSSessionLimitEnable False 4 1

KeyType IIsWebVirtualDir 4 1

Path D:\inetpub\wwwroot\Uploads 4 1

AppIsolated 0 4 1

AppRoot /LM/W3SVC/1/ROOT 4 1

BITSUploadEnabled True 4 1

BITSCleanupWorkItemKey {590e5102-9718-477b-bce0-d... 4 1

The current BITSMaximumUploadSize value is 18446744073709551615, which indicates that the default upload size limit is being used (on IIS 7.0 the default upload size limit is 30 million bytes). We can set this limit to a different value. After updating the value, we call the CommitChanges method so that the new configuration takes effect.

PS > $siteObj.BITSMaximumUploadSize = 10000000

PS > $siteObj.CommitChanges()

Finally, if we need to disable BITS uploads to the directory, we can call the DisableBitsUploads method.

PS > $siteObj.DisableBitsUploads()

 

Hope this helps!

Alex Ng

SDET – Windows Manageability

Comments

  • Anonymous
    March 08, 2016
    The comment has been removed