【问题标题】:How do I download all versions of a file in TFS with PowerShell?如何使用 PowerShell 在 TFS 中下载文件的所有版本?
【发布时间】:2019-04-03 17:03:49
【问题描述】:

我已安装 Visual Studio PowerTools,并正在尝试下载与每个变更集关联的物理文件。

我可以通过执行以下操作找到与文件关联的所有变更集并下载该文件的最新版本:

$tfsServer = Get-TFSServer 'http://myserver/tfs'
$history = Get-TfsItemHistory -HistoryItem $tfsHistoryItem -Server $tfsServer
$tfsProjColl = Get-TfsProjectCollection -Uri $tfsRootUrl
$tfsVersionControl = $tfsProjColl.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
foreach ($h in $history)
{
    $tempFilePath = [System.IO.Path]::GetTempFileName()
    $item = Get-TfsItemProperty -Item $h -Server $tfsServer
    $tfsVersionControl.DownloadFile($item.SourceServerItem, $tempFilePath)
}

起初,我以为我拥有它,但 SourceServerItem 只是对同一文件的引用,它是最新的。如何下载当时检查的文件版本?

【问题讨论】:

  • 我需要尝试一下,但您几乎可以肯定必须使用 VersionControlServer.DownloadFile Method (String, Int32, VersionSpec, String) 在其中指定特定的 VersionSpec(可能是 ChangeSetVersionSpec)你想要的版本
  • 我确实有那个下载部分。我只是不知道如何找到文件的每个版本。

标签: powershell tfs


【解决方案1】:

如果我正确理解了问题,那么这应该可以解决问题

$TpcUrl = "http://tfsserver:8080/tfs/DefaultCollection"
$filePath = "$/Burrito/ProjectA/Dev/AdminConsole/AdminConsole/Program.cs"
$tempFilePath = "C:\BurritoHistory\"
$tempFileName = "Program.cs"

[Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.VersionControl.Client')
[Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Client')

$tpc = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TpcUrl)
$vcs = $tpc.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

$rt = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
$history = $vcs.QueryHistory($filePath,$rt) 

foreach ($item in $history)
{
    Write-Host "Downloading Changeset " $item.ChangesetId

    $cvs =  New-Object Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec $item.ChangesetId
    $vcs.DownloadFile($filePath,0,$cvs,$tempFilePath + $item.ChangesetId + $tempFileName)   
}

这应该下载所选文件的多个版本,并以变更集 ID 为前缀。

【讨论】:

  • 谢谢! $versionControl 来自哪里?
  • 我明白了。看起来你打算在那里使用 $vcs 。做到了!
【解决方案2】:

如果您想列出来自 TFS 服务器的文件的每个版本。

您可以使用 TFS 对象模型中的 VersionControlServer.GetChangeset() 方法。示例代码

Private Shared Sub Main(ByVal args As String())
    Dim tfs As TfsTeamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(New Uri("http://tfsserver:8080/tfs/CollectionName"), New UICredentialsProvider)
    tfs.Connect(ConnectOptions.None)
    Dim vcs As VersionControlServer = tfs.GetService(Of VersionControlServer)
    Dim changeset As Changeset = vcs.GetChangeset(changeset ID, True, False) 
 End Sub

【讨论】:

  • 我正在使用 PowerShell,我需要实际下载物理文件;不仅仅是列出变更集。
【解决方案3】:

我已经更新了 rerwinX 答案中 powershel 中库的引用方式: 现在应该可以了:

$TpcUrl = "http://tfsserver:8080/tfs/DefaultCollection"
$filePath = "$/Burrito/ProjectA/Dev/AdminConsole/AdminConsole/Program.cs"
$tempFilePath = "C:\BurritoHistory\"
$tempFileName = "Program.cs"

Add-Type -Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.VersionControl.Client.dll'
Add-Type -Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Client.dll'

$tpc = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TpcUrl)
$vcs = $tpc.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

$rt = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
$history = $vcs.QueryHistory($filePath,$rt) 

foreach ($item in $history)
{
    Write-Host "Downloading Changeset " $item.ChangesetId

    $cvs =  New-Object Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec $item.ChangesetId
    $vcs.DownloadFile($filePath,0,$cvs,$tempFilePath + $item.ChangesetId + $tempFileName)   
}

【讨论】:

  • 对你的答案添加一些解释。
  • 在 rerwinX 中淘汰 dll 的方式:[Reflection.Assembly]::LoadWithPartialName 已弃用。所以我用正确的路径替换了它。有关更多详细信息,请参阅此post :)
猜你喜欢
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多