如何使用PowerShell在TFS中下载文件的所有版本?

时间:2016-01-14 20:34:20

标签: powershell tfs

我安装了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只是对同一个文件的引用,这是最新的。如何下载当时检查过的文件版本?

3 个答案:

答案 0 :(得分:2)

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

$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。

答案 1 :(得分:0)

如果要从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

答案 2 :(得分:0)

我已经更新了rerwinX Answers在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)   
}