Core/Projects.ps1

function Get-AzureDevOpsProjects()
{
  [CmdletBinding()]
  Param(
    [ValidateNotNullOrEmpty()]
    [Parameter(Mandatory=$true)]
    [string]$Url,

    [ValidateNotNullOrEmpty()]
    [Parameter(Mandatory=$true)]
    [string]$Collection,

    [psobject]$Headers = @{},

    [string]$PAT,

    [switch]$UseDefaultCredentials,

    [ValidateSet('all','createPending','deleted','deleting','new','unchanged','wellFormed')]
    [string]$StateFilter,

    [switch]$GetDefaultTeamImageUrl
  )

  Write-Debug ("Url: {0}" -f $Url)
  Write-Debug ("Collection: {0}" -f $Collection)
  Write-Debug ("Headers Length: {0}" -f $Headers.Length)
  Write-Debug ("PAT Length: {0}" -f $PAT.Length)
  Write-Debug ("UseDefaultCredentials: {0}" -f $UseDefaultCredentials)

  [psobject[]]$Projects = @{}
  [string]$ContinuationToken = ""

  [psobject]$Headers = Set-AuthorizationHeader -Password $PAT -Headers $Headers

  do
  {
    [psobject[]]$Results = @()

    [string]$Uri = "{0}/{1}/_apis/projects?api-version=5.0" -f $Url,$Collection

    if($StateFilter)            {$Uri += "&stateFilter={0}" -f $StateFilter}
    if($GetDefaultTeamImageUrl) {$Uri += "&getDefaultTeamImageUrl={0}" -f $GetDefaultTeamImageUrl.ToString()}

    if($ContinuationToken)
    {
      $Uri += "&continuationToken={0}" -f $ContinuationToken
    }
    
    Write-Verbose ("Uri: {0}" -f $Uri)

    $Results = Invoke-WebRequest -Uri $Uri -Headers $Headers -UseDefaultCredentials:$UseDefaultCredentials -UseBasicParsing
    $ContinuationToken = $Results.Headers.'x-ms-continuationtoken'
    $Projects += ($Results.Content | ConvertFrom-Json).value

  }while($ContinuationToken)

  Return $Projects
}