0

我有一个 PS 脚本,它从本地机器上的 TFS 下载最新代码,但我希望它下载特定标记代码而不是最新代码。

下面是下载 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  
    }  

我尝试使用Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec但没有成功。谁能指导我找到正确的链接或脚本,通过它我可以使用我在其上应用的标签下载“$/Match Web”代码。这是我在“$/Match Web”分支上应用的标签,例如 -“PreBuildLabel-MatchEnterpriseBuild1”

@Assael Azran,低于 $vs 的结果

在此处输入图像描述

4

1 回答 1

1

试试这个(对我有用):

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

$label - 你的标签名称

$scope - 标签的范围(项目)。要通过 VisualStudio 进行验证,请导航到File-> Source control-> Find-> Find Label。在“查找标签”表单中找到您的标签并打开它,然后您将看到项目名称(集合下的那个),您可以将其用作范围。

LabelVersionSpec 构造函数

更新

根据@SRP 的要求,您应该如何从 TFS 标签创建分支:

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

版本控制服务器.CreateBranch

于 2020-09-14T13:27:27.287 回答