Functions/PersonalAccessToken/Test-FpsAzDoPatFeedAccess.ps1

<#
.SYNOPSIS
    Test if a given Personal Access Token has access to a Azure DevOps Artifact Feed for a specific organisation.
.DESCRIPTION
    Test if a given Personal Access Token has access to a Azure DevOps Artifact Feed for a specific organisation.
    Returns true when there is access to the supplied feed. Returns false if there is no access.
    When there is no access the error will be logged in a warning message.
.EXAMPLE
    Test-FpsAzDoPatFeedAccess `
        -PersonalAccessToken (Get-FpsAzDoPat -AzDoPatMethod 'CredentialManager' -CredentialName 'PowershellPATAzureDevOps') `
        -AzDoFeedName 'fpstools'
.EXAMPLE
    Test-FpsAzDoPatFeedAccess `
        -PersonalAccessToken 'ff34885a8624460a8555y03w1shth12wa2va11d' `
        -AzDoOrganisation 'Dummy BV' `
        -AzDoProjectName 'ProjectName' `
        -AzDoFeedName 'PowerShellModules'
 
#>

function Test-FpsAzDoPatFeedAccess {
    [cmdletbinding()]
    Param (
        [parameter(Mandatory=$true)]
        [string] $PersonalAccessToken,
        [string] $AzDoOrganisation = '4psnl',
        [string] $AzDoProjectName = '4PS Tools',
        [string] $AzDoFeedName = 'fpstools_internal'
    )

    $azDoProjectsUri = 'https://dev.azure.com/{0}/_apis/projects?api-version=5.1' -f $AzDoOrganisation
    $feedBaseUri  = 'https://feeds.dev.azure.com/{0}' -f $AzDoOrganisation

    $header = 'Basic {0}' -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((':{0}' -f $PersonalAccessToken)))
    $header = @{Authorization = $header }

    # Retreive the Azure DevOps Project ID corresponding with the supplied project name. Required for the feed webrequest.
    try {
        $projects = (Invoke-RestMethod -Uri $azDoProjectsUri -Method get -Headers $header).value | Select-Object -Property id, name
    } catch {
        Write-Warning ('Web request failed to following uri: {0}' -f $azDoProjectsUri)
        Write-Warning $Error[0]
    }

    $projectId = ($projects | Where-Object -Property name -eq $AzDoProjectName).id

    if([string]::IsNullOrEmpty($projectId)){
        Write-Error ('Project ID for Azure DevOps project {0} could not be retreived with uri {1}' -f $AzDoProjectName, $azDoProjectsUri)
        return $false
    }

    $azDoFeedUri = '{0}/{1}/_apis/packaging/feeds?api-version=6.1-preview.1' -f 
                        $feedBaseUri, $projectId

    # Retreive available feeds from project
    try {
        $feeds = Invoke-RestMethod -Uri $azDoFeedUri -Method get -Headers $header
    } catch {
        Write-Warning ('Web request failed to following uri: {0}' -f $azDoFeedUri)
        Write-Warning $Error[0]
    }

    if ($feeds.count -gt 0) {
        'One or more feeds found: {0}' -f ($feeds.value.name | Out-String) | Write-Host
        
        if ($AzDoFeedName -in $feeds.value.name){
            'Access available to feed {0}' -f $AzDoFeedName | Write-Host
            return $true
        }
    }

    return $false
}

Export-ModuleMember -Function Test-FpsAzDoPatFeedAccess