1

Does anyone know how to write a PowerShell script to automate the "Get latest Version" function of Team Foundation Server?

I have tried several examples however none have worked.

Here is my code so far:

#Load Reference Assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")

Function Get-TFSLatestFromVersionControls
{
param
(
#FQDN to SCCM Server
[Parameter(Mandatory=$true)][string]$YourServerPath = "$/DEV",
[Parameter(Mandatory=$true)][string]$YourLocalPath = "C:\Project\DEV",
[Parameter(Mandatory=$false)][string]$tfsCollectionUrl = "http://10.0.10.200:8080/tfs/collection"
)

#Delete the old local copy
if((Test-Path -Path ($YourLocalPath)) -eq 1 )
{
    Remove-Item -Path $YourLocalPath -Recurse
    New-Item -Path $YourLocalPath -Type directory
Read-Host -Prompt "Delete the old local copy"
}


#Get Team Project Collection
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)

#enter a path to your tfs server
$tfsServer = $tfsCollectionUrl
 # get an instance of TfsTeamProjectCollection
$tfs=get-tfsserver $tfsServer
# get an instance of VersionControlServer
$vCS = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$TeamProject = $YourServerPath.Remove(0,2)
$tfsProject = $vcs.GetTeamProject($TeamProject)


$workspace = $vcs.GetWorkspace($YourLocalPath)
if($workspace -eq $null)
{
    $vcs.DeleteWorkspace("TFS-"+$env:COMPUTERNAME,$env:USERNAME)
    $workspace = $vcs.CreateWorkspace("TFS-"+$env:COMPUTERNAME, $env:USERNAME)
Read-Host -Prompt "Delete the old workspace"
}

$workspace.Map($YourServerPath, $YourLocalPath)

$workspace.Get([Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest ,[Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite)
Read-Host -Prompt "Get Latest"
}
LuidonVon
  • 443
  • 6
  • 20
  • Possible duplicate of [Get Latest Version of Folder from TFS, using Powershell](http://stackoverflow.com/questions/22298359/get-latest-version-of-folder-from-tfs-using-powershell) – Mark Wragg Apr 19 '17 at 10:11
  • No the duplicate did not solve it – LuidonVon Apr 19 '17 at 10:23
  • Have you tried writing any code? StackOverflow is not a code writing service. Show us what you have tried and we might be able to suggest solutions. – Mark Wragg Apr 19 '17 at 10:24
  • I have edited my post Mark – LuidonVon Apr 19 '17 at 10:35
  • You could use [Team Foundation Server Power Tools](https://marketplace.visualstudio.com/items?itemName=MartinWoodward.TeamFoundationServerPowerToolsDecember2011), Open the Windows PowerShell console, and then load the Team Foundation cmdlets from the Visual Studio, then you can use version control. More details please refer http://stackoverflow.com/questions/21299908/get-latest-version-of-a-project-from-tfs-using-powershell – PatrickLu-MSFT Apr 21 '17 at 01:51

1 Answers1

0

The simplest way to achieve this is installing TFS PowerTools as Patrick mentioned and then you just need to use "Update-TfsWorkspace" command to get the latest version. enter image description here

And if you want to get the latest via TFS API, refer to the code below for details:

$uri = "http://tfsurl:8080/tfs/collectionname/";
$workspacename = "workspacename"
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri)
$vcs = $tfs.GetService("Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
$workspaces = $vcs.QueryWorkspaces($workspacename,"","")
$workspace = $workspaces | Select-Object -First 1
$workspace.Get();
Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60