Friday, July 22, 2016

Copy entire files from TFS to local using PowerShell

In this script I am going to show how we can copy all the files available under a specified path to your local machine

  1. cls  
  2.   
  3. Write-Host "Enter source location "  // your tfs path
  4. $sourceLocation = Read-Host  
  5.   
  6. $tfsCollectionUrl = New-Object System.URI($sourceLocation);  
  7.   
  8. Write-Host "Enter server path "  // tfs source location $/MyFirstProject/
  9. $serverPath = Read-Host  
  10.   
  11. Write-Host "Enter local path to download"  // your local folder
  12. $localPath = Read-Host  
  13.   
  14. [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $tfsCollection = Get-TfsServer $tfsCollectionUrl  
  15.   
  16. $VersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])  
  17. $latest = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest  
  18. $recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full     
  19.   
  20. try  
  21. {  
  22.     foreach ($item in $VersionControl.GetItems($serverPath$latest,$recursionType).Items)  
  23.  {  
  24.     $target =   [io.path]::Combine($localPath,$item.ServerItem.Substring(2))  
  25.     $exists=[System.IO.Directory]::Exists($target)  
  26.  
  27.    if($item.ItemType -eq "Folder" -and !$exists)  
  28.    {  
  29.        New-Item $target -Type Directory  
  30.    }  
  31.    if($item.ItemType -eq "File")  
  32.    {  
  33.       $item.DownloadFile($target)  
  34.    }  
  35.   }  

  36. Write-Host "`n Successfully downloaded all the files to the target folder: " $localPath -ForegroundColor Green  
  37. }  
  38.     catch  
  39.     {  
  40.         $ErrorMessage = $_.Exception.Message  
  41.         $FailedItem = $_.Exception.ItemName  
  42.         Break  
  43.     }  

No comments:

Post a Comment

Popular Posts