0

I have Visual studio PowerTools installed and am trying to download the physical files that are associated with every changeset.

I can find all changesets associated with a file and downloaded the latest version of the file by performing the following:

$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)
}

At first, I thought I had it but SourceServerItem simply is a reference to the same file which is the latest. How can I download the version of the file that was checked at that time?

Adam Bertram
  • 3,858
  • 4
  • 22
  • 28
  • 1
    I'd need to try it out but you'll almost certainly have to use VersionControlServer.DownloadFile Method (String, Int32, VersionSpec, String) where you specify the VersionSpec (presumably ChangeSetVersionSpec) of the specific version you want – rerwinX Jan 15 '16 at 11:31
  • I do have that download part. I just can't figure out how to find each version of the fiile. – Adam Bertram Jan 15 '16 at 15:00

3 Answers3

2

If I understand the problem correctly then this should do the trick

$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)   
}

This should download multiple versions of the chosen file prefixed with the changeset id.

rerwinX
  • 2,025
  • 8
  • 9
0

If you want to list each version of a file from TFS server.

You could use the VersionControlServer.GetChangeset() method from the TFS Object Model. A Example code

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
PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
0

I have updated the way libraries are referenced in powershel from rerwinX answer : This should work now :

$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)   
}
Médéric
  • 1
  • 1
  • Add some explanation on your answer. – Mikev Apr 03 '19 at 09:06
  • The way dll were retireved in rerwinX : `[Reflection.Assembly]::LoadWithPartialName` is deprecated . So I replaced it with the new way with the right path. For more details please see this [post](https://stackoverflow.com/questions/12923074/how-to-load-assemblies-in-powershell) :) – Médéric Apr 04 '19 at 12:24