Copy files from tfs versioncontrol to directory with PowerShell

霸气de小男生 提交于 2019-11-28 10:25:21

I've created a PowerShell script that connects to the TFS server with the TFS assemblies. I then loop through the files on the server (in a specific path) and download it recursively.

# 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)))
    }
}

The PowerShell option is not selected by default in your when you install TFS Power Tools.

Select the option to install TFS powershell tools.

You can then use the Update-TfsWorkspace commandlet to get latest files. Please make sure you have a workspace already created for the directory where you need to latest files to get to. Then execute the following

cd <Your Directory Path>

Update-TfsWorkspace

While executing the command let, please make sure that that the workspace is created for the user that going to execute the powershell.

Just execute it on a

The command you are looking for is tf.exe get and that comes with the TFS client tools i.e. Visual Studio or the Team Explorer stand-alone tool. You will need to have a workspace and work folder mapping configured in order to use tf.exe get.

If you prefer to use the TF Power Tools then use the Update-TfsWorkspace command to get files. I believe you will still need a workspace defined with a workfold mapping.

Update-TfsWorkspace -Item $/TeamProject/Path/Path2/Item -Recurse
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!