Monday, June 11, 2012

Copying Files in the Background

In a previous tip we showed how you can use the BITS service to copy files. The main advantage of BITS is that it can copy things silently in the background, and it is resilient to interruptions such as reboots. After a reboot, BITS will continue the copy process, so this can be a good starting point to create a robust backup mechanism.
Import-Module BitsTransfer 

# adjust source and destination to your needs:

$Source
 = "$env:userprofile\downloads\*.exe"
$Destination
 = "C:\BackupFolder\" 

if
 ( (Test-Path $Destination-eq $false)
{
 $null = New-Item -Path $Destination -ItemType Directory
}


$params
 = @{
    
Source = $Source
    
Destination = $Destination
    
Description = "Backup"
    
DisplayName = "Backup"
    
Asynchronous = $true


Start-BitsTransfer
 @params
This script would copy all downloaded EXE files to a backup location. Since the backup is running in the background, you get back immediately a response like this:
JobId                                       DisplayName                      TransferType                     JobState                         OwnerAccount               
-----                                       -----------                      ------------                     --------                         ------------               
0488f61c-ab39-4661-81ce-b8f8a3... Backup                             Download                          Transferring                    TobiasAir1\Tobias 
To check progress, you can check progress using Get-BITSTransfer. You can call this cmdlet anytime, even next week after a couple of restarts. It may look like this:
PS> Get-BitsTransfer

JobId                                           DisplayName                   TransferType                     JobState                         OwnerAccount              
-----                                           -----------                   ------------                     --------                         ------------              
287ed32a-ec3a-4e13-b88d-9f288f...    Upload TUDI Data             Upload                             Transferred                     TobiasAir1\Tobias          
546c7a07-315b-4900-b1de-134aaa...    Upload TUDI Data             Upload                             Transferred                     TobiasAir1\Tobias          
0488f61c-ab39-4661-81ce-b8f8a3...    Backup                          Download                           Transferring                   TobiasAir1\Tobias     
You may see other BITS operations in this list as well since Windows is using the BITS service, too, to receive updates and upload data.
To actually complete the backup once it is done, you need to call Complete-BITSTransfer:
Import-Module BITSTransfer

Get-BitsTransfer
 |
  
Where-Object { $_.DisplayName -eq 'Backup' } |
  
ForEach-Object {   $_ | Select-Object -Property *

        
if ($_.JobState -eq 'Transferring') {
           
$transferred = $_.FilesTransferred  
           
$total = $_.FilesTotal
           
$text = '{0} of {1} files transferred. Try again later.' -f 
$transferred
$total
           
Write-Warning $text
     } 
else {
           
$done = $_.TransferCompletionTime
          
Complete-BitsTransfer $_
          
Write-Warning "Backup process done at $done"
     }
  }

0 comments:

Post a Comment

Tags

VMware (16) Rahim Shaikh (15) rahimshaikh.com (13) Scripts (7) Hack (6) Windows (6) ESXi (5) 2017 (3) AWS (3) Certification (3) Free (3) Resume (3) Storage (3) Windows 2016 (3) automation (3) vExpert 2017 (3) youtube (3) AD (2) Free dumps (2) Rahim (2) VMwar (2) Windows Search (2) disaster recovery (2) download (2) firewall (2) friend (2) snapshots (2) vExpert (2) vSphere (2) vcenter6 (2) windows2012 (2) 2018 (1) 2019 (1) 2k16 (1) 2k8 (1) ADDC (1) AWS Certified Solution Architect (1) Announcing the AWS Asia Pacific (Seoul) Region (1) Array Integration (1) Asia Pacific (1) BUG (1) Bangalore trip (1) Books (1) CMD (1) Cloud (1) DSRM (1) DefaultLogonDomain (1) Download URLs for vSphere Client (1) Exam (1) FTP (1) Guide (1) Hajj (1) IIS (1) KB940157 (1) LUN (1) Microsoft (1) Muslim (1) Problem (1) Pune (1) SRM (1) SRM 6.x (1) Saudi Arab Visit (Umrah) (1) Service (1) Umrah (1) VAAI (1) VCAP Dumps (1) VCAP5 (1) VCAP6 (1) VCDX (1) VCIX (1) VM Hardware (1) VMUG 2017 (1) VMware vSphere 6.5 (1) VMworld (1) VMworld 2017 (1) Vinod Balakrishnan (1) Vmware tools (1) Whats New (1) android (1) blogger (1) bloggger (1) certifi (1) death (1) documents (1) dumps (1) evaluation (1) event (1) guest customization (1) installation (1) job (1) knowledge (1) lab (1) letter (1) licence reset (1) license (1) life (1) linux (1) perennially-reserved=true (1) post (1) r2 (1) replication. (1) safe mode (1) script (1) sharing (1) technical preview (1) templates (1) travel (1) vCloud (1) vcenter (1) video (1) virtual machine (1) wife (1)