0

I need some help with how to clone all projects in my TFS 2015 (Update 3) collection at once (git). I found this powershell script below for a newer version, and I tried changing to API version from 4.0 to 2.0 but then it gets the error "Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'ForEach'."

Having trouble finding an example of how to do this clone in TFS 2015 API or any other way to do it.

$collection = "http://myserver:8080/tfs/DefaultCollection"

$projectsUrl = "$collection/_apis/projects?api-version=4.0"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -UseDefaultCredentials -ContentType application/json

$projects.value.ForEach({

  $reposUrl = "$collectionurl/$($_.name)/_apis/git/repositories?api-version=4.0"
  $repos = Invoke-RestMethod -Uri $reposUrl -Method Get -UseDefaultCredentials -ContentType application/json
  $repos.value.ForEach({
    git clone $_.remoteUrl --branch master --single-branch

  })
})
Brent Kilboy
  • 403
  • 4
  • 14

1 Answers1

0

I got it working. The first issue was that I needed to install a newer version of powershell.

After that it was just fixing a few issues in the script, here is the working script below.

$collection = "http://myserver:8080/tfs/DefaultCollection"

$projectsUrl = "$collection/_apis/projects?api-version=2.3-preview"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -UseDefaultCredentials -ContentType application/json -AllowUnencryptedAuthentication

$projects.value.ForEach({

  $reposUrl = "$collection/$($_.name)/_apis/git/repositories?api-version=2.3-preview"
  $repos = Invoke-RestMethod -Uri $reposUrl -Method Get -UseDefaultCredentials -ContentType application/json -AllowUnencryptedAuthentication
  $repos.value.ForEach({
    git clone $_.remoteUrl --branch master --single-branch

  })
})
Brent Kilboy
  • 403
  • 4
  • 14
  • 1
    Please [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), this can be beneficial to other community members reading this thread. – Cece Dong - MSFT Jun 01 '20 at 03:12