【问题标题】:Copy files from tfs versioncontrol to directory with PowerShell使用 PowerShell 将文件从 tfs 版本控制复制到目录
【发布时间】:2014-07-07 12:24:13
【问题描述】:

有人知道是否可以将文件从 TFS (2013 Update 2) 源代码管理复制到计算机上的特定文件夹?

假设我有服务器路径 $/BuildTemplate2013/BuildProcessSource,我希望使用 PowerShell 将该目录的所有文件复制/下载到 C:\destinationDir。那可能吗?我安装了 TFS 2013 Update 2 电动工具,但找不到任何命令...

【问题讨论】:

    标签: powershell tfs tfs-power-tools


    【解决方案1】:

    我创建了一个 PowerShell 脚本,它使用 TFS 程序集连接到 TFS 服务器。然后我遍历服务器上的文件(在特定路径中)并递归下载。

    # The deploy directory for all the msi, zip etc.
    $AutoDeployDir = "Your TFS Directory Server Path"
    $deployDirectory = $($Env:TF_BUILD_DROPLOCATION + "\Deploy\" + $Env:TF_BUILD_BUILDNUMBER)
    
    # Add TFS 2013 dlls so we can download some files
    Add-Type -AssemblyName 'Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    Add-Type -AssemblyName 'Microsoft.TeamFoundation.VersionControl.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    $tfsCollectionUrl = 'http://YourServer:8080/tfs/YourCollection' 
    $tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionUrl
    $tfsVersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
    
    # Register PowerShell commands
    Add-PSSnapin Microsoft.TeamFoundation.PowerShell
    
    # Get all directories and files in the AutoDeploy directory
    $items = Get-TfsChildItem $AutoDeployDir -Recurse
    
    # Download each item to a specific destination
    foreach ($item in $items) {
        # Serverpath of the item
        Write-Host "TFS item to download:" $($item.ServerItem) -ForegroundColor Blue
    
        $destinationPath = $item.ServerItem.Replace($AutoDeployDir, $deployDirectory)
        Write-Host "Download to" $([IO.Path]::GetFullPath($destinationPath)) -ForegroundColor Blue
    
        if ($item.ItemType -eq "Folder") {
            New-Item $([IO.Path]::GetFullPath($destinationPath)) -ItemType Directory -Force
        }
        else {
            # Download the file (not folder) to destination directory
            $tfsVersionControl.DownloadFile($item.ServerItem, $([IO.Path]::GetFullPath($destinationPath)))
        }
    }
    

    【讨论】:

    • 我必须在 $items = Get-TfsChildItem $AutoDeployDir -Recurse 之后添加 -Server $tfsCollection 才能运行它,然后它就像冠军一样工作。
    • 也许是因为我们这家公司只有一个 Collection 是我不需要它的原因?我为 TFS2013 做了这个
    • 啊,我在 2010 年 - 不管怎样,感谢脚本,它为我节省了很多时间。我只是想提一下,以防其他人遇到类似问题。
    • 我正在尝试使用您的脚本,我正在从example.visualstudio.com/DefaultCollection 克隆存储库,我应该在 AutoDeployDir 中放入什么。我的克隆网址是example.visualstudio.com/DefaultCollection/playground/_git/…。我已经尝试了完整路径,使用 $/playground 和其他组合直到最终文件夹,但项目列表始终为空
    • @RalphJansen - 太棒了。我现在看到它正在使用来自 appdata/cache 的凭据进行 TFS。您是否知道我们可以使用哪些库在脚本本身中传递特定凭据,而不是使用本地缓存中的凭据?
    【解决方案2】:

    在您安装 TFS Power Tools 时,默认情况下不会选择 PowerShell 选项。

    选择安装 TFS powershell 工具的选项。

    然后您可以使用 Update-TfsWorkspace 命令行开关获取最新文件。请确保您已经为需要获取最新文件的目录创建了工作区。然后执行以下

    cd <Your Directory Path>
    
    Update-TfsWorkspace
    

    在执行 let 命令时,请确保为将要执行 powershell 的用户创建了工作区。

    只需在 a 上执行它

    【讨论】:

    • 但是我怎样才能在我的驱动器上指定一个特定的目录,比如C:\destinationDir?我正在从我的构建过程模板中调用此脚本,并且需要更改一些文件。
    • 其中一种方法是从要从中复制它的目录执行 PowerShell 命令行开关。您需要已经为该文件夹设置了工作区映射才能执行此操作。
    • 该命令仅列出服务器的内容。它相当于tf dir。它实际上并不将文件检索到本地文件系统中。
    • 你是对的。道歉!!您需要运行命令行开关“Update-TfsWorkspace”。只需在已设置工作空间的目录上执行它而不使用任何参数。我会更新我的答案。
    • 感谢您提供信息,但我创建的脚本略有不同,因为我不想使用工作区。
    【解决方案3】:

    您要查找的命令是 tf.exe get,它随 TFS 客户端工具(即 Visual Studio 或 Team Explorer 独立工具)提供。您需要配置工作区和工作文件夹映射才能使用tf.exe get

    如果您更喜欢使用 TF Power Tools,请使用Update-TfsWorkspace 命令获取文件。我相信您仍然需要使用工作文件夹映射定义的工作空间。

    Update-TfsWorkspace -Item $/TeamProject/Path/Path2/Item -Recurse
    

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 2012-04-17
      • 1970-01-01
      • 2016-04-20
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多