xcodr
xcodr

Reputation: 1177

How to get certain files from TFS using PowerShell?

After we branch for release, there are certain configuration files that need to be updated. Currently, I'm getting latest version of all the source code on the specific (new) branch. This is time consuming and takes up a lot of disk space.

Is there a way to search/loop through the particular branch and only getting files that match a certain criteria?

E.g:

Loop through files under branch on the TFS server
If file name matches "foo.proj" or file name matches "foo.ini" 
Get latest version of the file

The "get" part is easy using the command line interface to TF (tf get $/project/foo.proj). I'm not to sure how to loop through the server objects without getting latest version first.

Upvotes: 0

Views: 8038

Answers (2)

Richard
Richard

Reputation: 109170

Take a look at the TFS PowerShell Snapin from the TFS PowerToys (you need a custom install to select).

After loading the snapin in a 32bit PSH instance (Add-PSSnapin Microsoft.TeamFoundation.PowerShell) you can use:

  • Get-TFSItemProperty on a folder to get a list of items under source control.
  • Update-TfsWorkspace to get or update all or specific files/folders into your workspace.

Update: You can install the PSH TFS snapin for 64bit yourself having installed it for 32bit via the PowerToys (see comments) in which case this is not limited to a 32bit PSH instance.

Upvotes: 2

xcodr
xcodr

Reputation: 1177

I used this in the end:

Custom install the TFS Powershell snap in using the tftp.exe that you download.

Add the snap in:

if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}

Powershell script:

param( [string] $ServerBranchLocation )

$tfs=Get-TfsServer -name http://mytfsserver:8080/tfs
$TfExePath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe"

$matchFoo = "foofile.foo"

foreach ($item in Get-TfsChildItem $ServerBranchLocation -r -server $tfs) 
{ 
    if ($item -match $matchFoo)
    { & "$TFExePath" get $item.ServerItem /force /noprompt }
}

I couldn't find a way to get the latest version using the snap in, so I had to use the ol' trusty tf.exe.

Upvotes: 1

Related Questions