0

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

SRP
  • 999
  • 4
  • 21
  • 39

1 Answers1

1

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

Assael Azran
  • 2,863
  • 1
  • 9
  • 13
  • The above code is giving me the below error in foreach for GetItems :- Cannot find an overload for "GetItems" and the argument count: "3". At C:\Suraj\POC PostGA\Label.ps1:37 char:19 + ... h ($item in $VersionControl.GetItems($serverPath, $vs,$recursionType) ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest – SRP Sep 14 '20 at 14:26
  • Are you getting any results to `$vs`? can you share what you did? – Assael Azran Sep 14 '20 at 14:46
  • yes, I have added the screenshot of value above that is getting added. I simply added value to $label and $scope and copied the line in your answer $vs = New-Object Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec($label, $scope); – SRP Sep 14 '20 at 14:48
  • Which dll version of Microsoft.TeamFoundation.VersionControl.Client are you using? if you run `$VersionControl.GetItems($serverPath, $vs,$recursionType).Items` are you getting anything? – Assael Azran Sep 14 '20 at 15:16
  • I am not sure, I completely understood your above query but i guess you want to know the values of the following variables present inside GetItems($serverPath, $vs,$recursionType). The $serverpath = "$/IntelliMatch Web/Release/NextGen_v20.2_GA", the value for $vs is already shared above in screenshot and $recursionType seems to be empty. The Microsoft.TeamFoundation.VersionControl.Client dll is of VS 2017 and it shows the file version as 15.129.28306.1 in its properties. – SRP Sep 14 '20 at 15:19
  • @ Assael Azran by bad, I missed this line from the script, "$recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full " It is working now, thank you!! – SRP Sep 14 '20 at 15:22
  • @ Assael Azran instead of downloading the labelled code, if I want to create a new branch in TFS of the labelled branch is that possible? Where can I find the PS commands for the same, any link? – SRP Sep 16 '20 at 05:31
  • Thank you Assael Azran!! – SRP Sep 16 '20 at 14:59