0

I'm trying to create a Powershell script that will setup a brand new workspace in a temporary location, do a GetLatest on selected solutions/projects, and download the source code so that I can then trigger further build/versioning operations.

I think I have the script more or less right, but the problem is every time I run this, it tells me there were 0 operations... i.e. I already have the latest versions. This results in nothing at all being downloaded.

Can anyone see what I'm doing wrong?

$subfolder = [System.Guid]::NewGuid().ToString()
$tfsServer = "http://tfsserver:8080/tfs"
$projectsAndWorkspaces = @(
    @("$/Client1/Project1","D:\Builds\$subfolder\Client1\Project1"),
    @("$/Client1/Project2","D:\Builds\$subfolder\Client1\Project2"),
)


$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsServer)
$tfsVersionCtrl = $tfsCollection.GetService([type] "Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
$tfsWorkspace = $tfsVersionCtrl.CreateWorkspace($subfolder, $tfsVersionCtrl.AuthorizedUser)

Write-Host "Operations:"

foreach ($projectAndWs in $projectsAndWorkspaces)
{
    if (-not(Test-Path $projectAndWs[1]))
    {
        New-Item -ItemType Directory -Force -Path $projectAndWs[1] | Out-Null
    }

    $tfsWorkspace.Map($projectAndWs[0], $projectAndWs[1])
    $recursion = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
    $itemSpecFullTeamProj = New-Object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($projectAndWs[0], $recursion)
    $fileRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($itemSpecFullTeamProj, [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest)
    $getStatus = $tfsWorkspace.Get($fileRequest, [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite)
    Write-Host ("[{0}] {1}" -f $getStatus.NumOperations, ($projectAndWs[0].Substring($projectAndWs[0].LastIndexOf("/") + 1)))
}

Write-Host "Finished"
Detail
  • 785
  • 9
  • 23
  • If you look in source code explorer what does it show about whether you have the latest? TFS-VC tracks on the server what versions of what files are in a workspace. If you manually delete files it does not know and will not download them. You might need to force a download to get everything. – Richard Mar 31 '17 at 09:16
  • @Richard I'm creating a brand new workspace though... surely it shouldn't have any files when first created? – Detail Mar 31 '17 at 09:17
  • Bang goes that theory! – Richard Mar 31 '17 at 09:34
  • 1
    The `$tfsServer = "http://tfsserver:8080/tfs"` should be `$tfsServer = "http://tfsserver:8080/tfs/nameOfACollection"` – Giulio Vian Mar 31 '17 at 13:31
  • That's the one! Thanks mate, change this to an answer and I'll mark it as the solution – Detail Mar 31 '17 at 14:13
  • @GiulioVian please post your comment as the answer. – Tingting0929 Apr 03 '17 at 03:03

2 Answers2

1

The $tfsServer = "http://tfsserver:8080/tfs" should be $tfsServer = "http://tfsserver:8080/tfs/nameOfACollection"

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
0

The "$/Client1/Project1" string smells. I would add a backtick before the dollar sign so it is not read as a variable or use single quotes.

Backtick "`$/Client1/Project1" Single quote '$/Client1/Project1'

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41