问题
I've look through many posts but still don't get it. I need a powershell script to download latest version of a specific project.
Let's say I just want to download the latest copies of files from ProjectA branch dfe. I saw some PS script that deletes the old files and download all the files again but that takes too long. script that would overwrite existing files would be much better. I tried update-tfsworkspace, it downloads everything but I don't want to download everything. server: http://servername:8080/tfs/DefaultCollection
DefaultCollection ProjectA -Folder1 - branch dfe -Folder2 - branch abc
ProjectB -Folder1 - branch - branch
回答1:
First you need a good workspace mapping. You can create one manually, or from an xml file as below. Also, the functionality you are after to overwrite is [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite
function Get-ProjectXml {
[CmdletBinding()]
Param(
[string] $tfs13Url,
[string] $workspaceName ,
[string] $workspaceDescription,
[string] $fileName,
[bool] $autoDeleteCreate)
try
{
#Connect to TFS:
$tfs13 = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfs13url)
$vcs13 = $tfs13.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$workspaces=$vcs13.QueryWorkspaces($workspaceName, $vcs13.AuthenticatedUser, [Environment]::MachineName);
if($autoDeleteCreate ){
#Delete if Workspace exists, as this is an automated process...
if ($workspaces.count -gt 0)
{
$vcs13.DeleteWorkspace($workspaceName, $vcs13.AuthenticatedUser);
}
$workspace = $tfs10.VCS.CreateWorkspace($workspaceName, $vcs13.AuthenticatedUser, $workspaceDescription);
}
else
{
$workspace=$workspaces[0]
}
#Get XML File
[xml]$xmlMappings= New-Object System.Xml.XmlDocument
$xmlMappings.Load($fileName)
$rootLocalItem=$xmlMappings.Root.LocalItem
#Iterate through each collection
foreach($collection in $xmlMappings.Collection){
write-host "Mapping project $($collection.Name)"
#Iterate through each project
foreach($project in $collection.Project){
write-host "Mapping project $($project.Name)"
foreach($mapping in $project.Mapping){
$workspace.Map($mapping.ServerItem, $mapping.LocalItem);
}
}
}
$resultTime= Measure-Command {
$result=$workspace.Get([Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest,[Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite)
}
$resultAlerts=$result.NumConflicts+ $result.NumFailures+ $result.NumWarnings
if ($resultAlerts -eq 0) {
Write-host "Get Operation completed without any errors. $($result.NumUpdated) files are updated on workspace: $($workspace.name)" }
else {
Write-Host "Get Operation completed with errors. Please check errors: $($result.GetFailures)"}
$resultTime.ToString()
}
catch
{
return $_.exception
}
}
[string] $tfs13Url="http://tfs:8080/tfs/defaultcollection",
[string] $workspaceName = "AutomatedWorkspace",
[string] $workspaceDescription = "AutomatedWorkspace",
[string] $fileName="D:\projects\ProjectMapping.xml",
[bool] $autoDeleteCreate =$true
Get-ProjectXml $tfs13Url $workspaceName $workspaceDescription $fileName $autoDeleteCreate
Assuming your xml is like:
<?xml version="1.0"?>
<Collection Name="DefaultCollection">
<Root>
<ServerItem>$/</ServerItem>
<LocalItem>d:\Src</LocalItem>
</Root>
<Project name="ProjectA">
<Mapping>
<ServerItem>$/ProjectA/Folder1/BranchDEF</ServerItem>
<LocalItem>d:\Src\ProjectA\Folder1\BranchDEF\</LocalItem>
</Mapping>
<Mapping>
<ServerItem>$/ProjectA/Folder2\BranchABC</ServerItem>
<LocalItem>d:\Src\ProjectA\Folder2\BranchABC</LocalItem>
</Mapping>
</Project>
<Project Name="ProjectB">
<Mapping>
<ServerItem>$/ProjectB/Folder5\BranchXYZ</ServerItem>
<LocalItem>d:\Src\ProjectB\Folder5\BranchXYZ</LocalItem>
</Mapping>
</Project>
</Collection>
来源:https://stackoverflow.com/questions/33404043/how-to-get-latest-version-of-specific-project-in-tfs-using-powershell