DevOpsHandling/Get-RepositoryId.ps1

<#
 .Synopsis
  Gets the Id of a DevOps repository
 .Description
  Gets the Id of a DevOps repository
 .Parameter devOpsOrganization
  The DevOps organization that contains the artifacts
  .Parameter devOpsProjectName
  The name of the DevOps project
  .Parameter repositoryName
  The name of the DevOps repository
  .Parameter devOpsToken
  PAT for DevOps
  .Example
  Get-RepositoryId -devOpsOrganisation "test" -devOpsProjectName "test project" -repositoryName "name" -devOpsToken "00000000-0000-0000-0000-000000000000"
#>

function Get-RepositoryId {
    Param (
        [Parameter(Mandatory=$true)]
        [string]$devOpsOrganization,
        [Parameter(Mandatory=$true)]
        [string]$devOpsProjectName,
        [Parameter(Mandatory=$false)]
        [string]$repositoryName,
        [Parameter(Mandatory=$true)]
        [string]$devOpsToken
    )

    $repos = (Invoke-AzureDevOpsApi -url ('https://dev.azure.com/{0}/{1}/_apis/git/repositories' -f $devOpsOrganization, $devOpsProjectName) -devOpsToken $devOpsToken)

    try {
        if ($repositoryName -ne '') {
            $id = ($repos.value | Where-Object name -like ('*{0}*' -f $repositoryName)).id
        }
        else {
            $id = $repos.value.item(0).id
        }

        if ($id -eq '' -or $null -eq $id) {
            $id = Get-RepositoryId -devOpsOrganization $devOpsOrganization -devOpsProjectName $devOpsProjectName -repositoryName '' -devOpsToken $devOpsToken
        }
    } catch {
        throw "Could not find repository id for $repositoryName"
    }

    return $id
}