Aller à la navigation Aller à la recherche

BITS, exemple 1

Source : https://www.it-connect.fr

	Download files recursively with BITS with the same arborescence.

    This script download files recursively using BITS transfer method. 
    It recreate the arborescence of the source in the destination folder, and download each file at the same place that in the source.

    The path to the source folder, all files in his subfolders will be copied. PLEASE, DO NOT INCLUDE A BACKSLASH AT THE END OF THE PATH

.PARAMETER Destination
    The path to the destination folder, where you want to store downloaded files. PLEASE, DO NOT INCLUDE A BACKSLASH AT THE END OF THE PATH

    .\Start-BitsDownloadRecursive.ps1 -Source "\\\Download\BITS\" -Dest "C:\temp\BITS"

	NAME:	Start-BitsDownloadRecursive.ps1
	AUTHOR:	Florian Burnel
	EMAIL:	florian.burnel@it-connect.fr
	WWW:	www.it-connect.fr


	1.0 	2017.02.02
		    Initial Version

    * Include a parameter for credentials
    * Improve control over transfers
    * Add possibility to exclude folders


### Parameters
param (
    [parameter(Mandatory=$true)][ValidateScript({Test-Path $_ })][String]$Source,
    [parameter(Mandatory=$true)][ValidateScript({Test-Path $_ })][String]$Dest

# Import module BitsTransfer
Import-Module BitsTransfer -ErrorAction SilentlyContinue -ErrorVariable ModuleState

# Continue only if the module is correctly imported
If (!($ModuleState)) {
    Get-ChildItem $Source -Recurse -Directory | ForEach-Object {
        # Start the download of files which are in the root of the source
        If ((Get-BitsTransfer -Name "Root-Source" -ErrorAction SilentlyContinue).Count -eq 0) {
            Start-BitsTransfer -Asynchronous -Source "$Source\*.*" -Destination $Dest -DisplayName "Root-Source"

        # Path to the child item (directory)
        $DestChild = ($_.FullName).Replace($Source, "")
        # If the directory in the destination is already created, start the BITS transfer, else it will be create before start the job.
        If (Test-Path "$Dest\$DestChild\") {
            Start-BitsTransfer -Asynchronous -Source "$($_.FullName)\*.*" -Destination "$Dest\$DestChild\" -DisplayName $_.Name
        Else {
            New-Item -ItemType Directory -Path "$Dest\$DestChild" -ErrorVariable FolderCreation
            If (!($FolderCreation)) {
                Start-BitsTransfer -Asynchronous -Source "$($_.FullName)\*.*" -Destination "$Dest\$DestChild\" -DisplayName $_.Name            
            Else {
                Write-Output "ERROR ! Impossible to create the destination folder ($DestChild) !"
                } # if(!($FolderCreation))

        } # if(Test-Path "$Dest\$DestChild\")

    # Wait during the BITS transfer before to complete it
    While (((Get-BitsTransfer).JobState -eq "Transferring") -or ((Get-BitsTransfer).JobState -eq "Connecting")) {
        Start-Sleep -Seconds 3

    # Complete all the BITS transfer
    Get-BitsTransfer | Complete-BitsTransfer

    # Check that the source and the destination are the same number of elements
    If ((Get-ChildItem $Source -Recurse).Count -eq (Get-ChildItem $Dest -Recurse).Count) {
        Write-Output "Transfer complete ! All elements are transferred !"
    Else {
        Write-Output "ERROR ! One or several elements aren't transferred !"
Else {
    Write-Output "ERROR ! Impossible to load the module BitsTransfer"
    } # if(!($ModuleState))

Voir aussi