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

标签 powershell tfs

我浏览了很多帖子,但仍然听不懂。我需要一个Powershell脚本来下载特定项目的最新版本。

假设我只想从ProjectA分支dfe下载文件的最新副本。我看到了一些PS脚本,该脚本删除了旧文件并再次下载了所有文件,但这花费的时间太长。覆盖现有文件的脚本会更好。我尝试了update-tfsworkspace,它下载了所有内容,但我不想下载所有内容。
伺服器:http://servername:8080/tfs/DefaultCollection

DefaultCollection
项目A
-文件夹1
-分支DFE
-文件夹2
-分支abc

项目B
-文件夹1
-分支
-分支

最佳答案

首先,您需要一个良好的工作区映射。您可以手动创建一个,也可以从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> 

关于powershell - 如何使用Powershell在TFS中获取特定项目的最新版本。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404043/

相关文章:

azure - 将带有标签的 Azure 资源组导出到 csv

azure - 如何使用 powershell 从 Azure 表存储中删除属性?

powershell - 迭代并搜索子项中的字符串

visual-studio-2010 - 用于 TFS 构建中包含的解决方案的 NuGet

c# - 为什么我在 Powershell 和 C# 之间得到不同的结果

PowerShell:Start-Job 中的 $input 到底是什么?

visual-studio - TFS 工作项查询策略不会在 Visual Studio 上刷新

tfs - 如何找出谁运行了 TFS 销毁命令?

version-control - TFS 2012 版本树 - ClearCase 像?

tfs - 如何在没有 'getting' 项目的情况下在 TFS 中搜索单词?