如何使用Powershell在TFS中获取特定项目的最新版本。

时间:2015-10-29 00:37:07

标签: powershell tfs tfs2013

我查看过很多帖子,但仍然没有得到它。我需要一个powershell脚本来下载特定项目的最新版本。

让我们说我只想从ProjectA分支机构下载最新的文件副本。我看到一些PS脚本删除旧文件并再次下载所有文件,但这需要太长时间。将覆盖现有文件的脚本会好得多。我尝试了update-tfsworkspace,它下载了所有内容,但我不想下载所有内容。 服务器:http://servername:8080/tfs/DefaultCollection

DefaultCollection  项目A  -Folder1    - 分支dfe  -Folder2    - 分支abc

项目B  -Folder1    - 分支    - 分支

1 个答案:

答案 0 :(得分:3)

首先,您需要一个良好的工作区映射。您可以手动创建一个,也可以从xml文件创建,如下所示。此外,您要覆盖的功能是[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

假设您的xml如下:

   <?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>