powershell - 使用 PowerShell 将文件从 tfs 版本控制复制到目录

标签 powershell tfs tfs-power-tools

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

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

最佳答案

我创建了一个 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)))
    }
}

关于powershell - 使用 PowerShell 将文件从 tfs 版本控制复制到目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739499/

相关文章:

powershell - powercli Get-VM 不匹配标签

powershell - 文件的哈希表不会对值进行 PsIsContainer 测试

python - 安装3.5后如何从win7 powershell启动python 2.7?

tfs - 从 Jira 导入数据到 TFS 2015

linux - 构建 Electron linux 发行版 : The SUID sandbox helper binary was found, 但未正确配置

visual-studio-2010 - 源代码管理无法启动手动合并工具,为什么?

tfs - TF.exe 相当于 TFPT.exe scorch

powershell - ffmpeg与Powershell的cmd等效

xcode - 获取 TF205022 : The following path contains more than the allowed 259 characters in Xcode and TFS

tfs - 给定 TFS 变更集,我如何找到它链接到的工作项?