« PowerShell/Fichier/BITS » : différence entre les versions

Aller à la navigation Aller à la recherche
(Page créée avec « {{Sommaire}} == BITS, exemple 1 == Source : https://www.it-connect.fr <source> <# .SYNOPSIS Download files recursively with BITS with the same arborescence. .DESCRIPTION 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. .PARAMETER Source The path to the source folder, all files in his subfolders will... »)
 
mAucun résumé des modifications
 
Ligne 45 : Ligne 45 :


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


# Import module BitsTransfer
# Import module BitsTransfer
Ligne 54 : Ligne 54 :


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


         # Path to the child item (directory)
         # Path to the child item (directory)
         $DestChild = ($_.FullName).Replace($Source,"")
         $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 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\"){
         If (Test-Path "$Dest\$DestChild\") {
 
             Start-BitsTransfer -Asynchronous -Source "$($_.FullName)\*.*" -Destination "$Dest\$DestChild\" -DisplayName $_.Name
             Start-BitsTransfer -Asynchronous -Source "$($_.FullName)\*.*" -Destination "$Dest\$DestChild\" -DisplayName $_.Name
 
            }
         }else{
         Else {
 
             New-Item -ItemType Directory -Path "$Dest\$DestChild" -ErrorVariable FolderCreation
             New-Item -ItemType Directory -Path "$Dest\$DestChild" -ErrorVariable FolderCreation
       
             If (!($FolderCreation)) {
             if(!($FolderCreation)){
                 Start-BitsTransfer -Asynchronous -Source "$($_.FullName)\*.*" -Destination "$Dest\$DestChild\" -DisplayName $_.Name          
           
                }
                 Start-BitsTransfer -Asynchronous -Source "$($_.FullName)\*.*" -Destination "$Dest\$DestChild\" -DisplayName $_.Name
             Else {
           
             }else{
 
                 Write-Output "ERROR ! Impossible to create the destination folder ($DestChild) !"
                 Write-Output "ERROR ! Impossible to create the destination folder ($DestChild) !"
           
                } # if(!($FolderCreation))
            } # if(!($FolderCreation))


         } # if(Test-Path "$Dest\$DestChild\")
         } # if(Test-Path "$Dest\$DestChild\")
Ligne 91 : Ligne 81 :


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


     # Complete all the BITS transfer
     # Complete all the BITS transfer
Ligne 101 : Ligne 89 :


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



Dernière version du 11 octobre 2023 à 22:24

BITS, exemple 1

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

<#
.SYNOPSIS
	Download files recursively with BITS with the same arborescence.

.DESCRIPTION
    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.

.PARAMETER 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

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

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

	VERSION HISTORY:

	1.0 	2017.02.02
		    Initial Version

    TODO
    * 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

  •