簡體   English   中英

將文件從 tfs 版本控制復制到帶有 PowerShell 的目錄

[英]Copy files from tfs versioncontrol to directory with PowerShell

有人知道是否可以將文件從 TFS (2013 Update 2) 源代碼管理復制到計算機上的特定文件夾?

假設我有服務器路徑$/BuildTemplate2013/BuildProcessSource並且我希望將該目錄的所有文件復制/下載到C:\destinationDir和 PowerShell。 那可能嗎? 我安裝了 TFS 2013 Update 2 電動工具,但我找不到任何命令...

我已經創建了一個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)))
    }
}

安裝TFS Power Tools時,默認情況下不會選擇PowerShell選項。

選擇安裝TFS powershell工具的選項。

在此輸入圖像描述

然后,您可以使用Update-TfsWorkspace命令行開關獲取最新文件。 請確保您已為要獲取最新文件的目錄創建了工作區。 然后執行以下操作

cd <Your Directory Path>

Update-TfsWorkspace

執行命令let時,請確保為要執行powershell的用戶創建工作區。

只需在a上執行即可

您正在尋找的命令是tf.exe get ,它附帶了TFS客戶端工具,即Visual Studio或Team Explorer獨立工具。 您需要配置工作空間和工作文件夾映射才能使用tf.exe get

如果您更喜歡使用TF Power Tools,請使用Update-TfsWorkspace命令獲取文件。 我相信你仍然需要一個用workfold映射定義的工作空間。

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

我用的是 rest api

    $webClient = New-Object System.Net.WebClient

    $fileName = "<FILENAME>"
    $tfsMappingPath = "$/<TFS_MAPPING>/$fileName"
    $url = "<TFS_URL>/_apis/tfvc/items?path=$tfsMappingPath"

    $WebClient.UseDefaultCredentials = $true

    $WebClient.DownloadFile($url,"$pwd/$fileName")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM