powershelltfsazure-devopspowershell-5.0tfs-2017

How to download a specific labelled code from TFS using PowerShell Script?


I have a PS script which downloads the latest code from TFS on my local machine but I want it to download a specific labelled code instead of latest.

Below is the script which downloads the latest code present in TFS,

$sourceLocation = "http://vwmaztfsapp:8080/tfs/MatchCollection"
  
$tfsCollectionUrl = New-Object System.URI($sourceLocation);  
  
$serverPath = "$/Match Web/Installscript Projects/Utility Scripts"  
  
#It gets copied at local path with the above folder sequence
$localPath = "C:\"

    
$visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
    Add-type -path "$visualStudiopath\Microsoft.TeamFoundation.ProjectManagement.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Build.Common.dll"


$tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionUrl  

$VersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])  
$latest = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest  
$recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full     
  
  
    try  
    {  
  
        foreach ($item in $VersionControl.GetItems($serverPath, $latest,$recursionType).Items)  
        {  
            $target =   [io.path]::Combine($localPath,$item.ServerItem.Substring(2))  
            $exists=[System.IO.Directory]::Exists($target)  
  
            if($item.ItemType -eq "Folder" -and !$exists)  
            {  
                New-Item $target -Type Directory  
            }  
            if($item.ItemType -eq "File")  
            {  
                $item.DownloadFile($target)  
            }  
        }  
        Write-Host "`n Successfully downloaded all the files to the target folder: " $localPath -ForegroundColor Green  
    }  
    catch  
    {  
        $ErrorMessage = $_.Exception.Message  
        $FailedItem = $_.Exception.ItemName  
        Break  
    }  

I tried using the Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec but was not successful. Can anyone please guide me to the correct link or script by which I can download the "$/Match Web" code using the label which I had applied on it. This is the label which I had applied on "$/Match Web" branch for e.g. - "PreBuildLabel-MatchEnterpriseBuild1"

@Assael Azran, getting below result in $vs

enter image description here


Solution

  • Try this (works for me):

    $vs = New-Object Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec($label, $scope);
    foreach ($item in $VersionControl.GetItems($serverPath, $vs,$recursionType).Items)  
    {
      .....
    }
    

    $label - name of your label

    $scope - The scope (project) of the label. To verify that through VisualStudio, navigate to File-> Source control-> Find-> Find Label. In the "Find Label" form find your label and open it, then you will see the project name (the one under the collection), you can use it as the scope.

    LabelVersionSpec Constructor

    UPDATE

    Upon request of @SRP, this is how you should create a branch from a TFS label:

    $vcs = $server.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]); 
    $changesetId = $vcs.CreateBranch($sourceBranch, $destBranch,$vs)
    

    VersionControlServer.CreateBranch