A quick follow up on my last post regarding TFS and PowerShell. I found it quite amazing that is so little documented and relatively difficult to download a file from TFS to your local machine. I would have expected that the supplied Cmdlets in the TFS Power Tools snapin will just handle that for you, as I find the requirement to download a file not too exotic at all. Or maybe I am doing something wrong? anyway, in case you are interested, this is how it goes…
Param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $FileName
,
[Parameter(Mandatory = $false, Position = 1)]
[string] $LocalPath = $PWD
,
[Parameter(Mandatory = $false)]
[string] $TFSServer = 'https://tfs.example.com/defaultcollection'
,
[Parameter(Mandatory = $false)]
[string] $Workspace = 'WORKSPACE'
,
[Parameter(Mandatory = $false)]
[string] $Owner = "Edgar Schnittenfittich"
,
[ValidateScript( { Test-Path -Path $_; } )]
[Parameter(Mandatory = $false)]
[string] $WorkingFolder = 'C:\Prj\WorkingFolder'
,
[Parameter(Mandatory = $false)]
[PSCredential] $Credential = (Import-CliXml C:\Prj\Credential-tfs.xml)
)
Push-Location $WorkingFolder;
Add-PSSnapin Microsoft.TeamFoundation.PowerShell;
Write-Host ("Login to TFS '{0}' ..." -f $TfsServer);
$tfs = Get-TfsServer $TFSServer -Credential $Credential;
Write-Host ("Getting workspace '{0}' ..." -f $Workspace);
$tfws = Get-TfsWorkspace -Server $tfs -Owner $Owner -Name $Workspace
Write-Host ("Getting Version Control Server Information from '{0}' ..." -f $TFS.Name);
$vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
Write-Host ("Getting item '{0}' from TFS ..." -f $FileName);
$i = New-Object Microsoft.TeamFoundation.PowerTools.PowerShell.QualifiedItemSpec($FileName)
$aItem = Get-TfsChildItem $i -Recurse;
foreach($item in $aItem) {
$sitem = Select-TfsItem $item
$LocalPathAndFileName = Join-Path -Path $LocalPath -ChildPath $FileName;
$vcs.DownloadFile($sitem.Path, $LocalPathAndFileName);
} # foreach
Note 1: you have to specify a working folder mapped and specified in the script, otherwise you might get an error similar to this:
There is no working folder mapping for class1.cs.
Note 2: In case you do not specify a local folder where to download, the file will be put into your home directory (and not the current directory).
