Public/Get-PSUADOProjectList.ps1

#TODO: add comment based help
function Get-PSUADOProjectList {
    [CmdletBinding()]
    param (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$Organization = $env:ORGANIZATION,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$PAT = $env:PAT
    )

    process {
        try {
            Write-Host "Fetching the Projects in the organization [$Organization]"
            $uri = "https://dev.azure.com/$Organization/_apis/projects?api-version=7.0"
            $headers = @{
                Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$PAT"))
            }

            $response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get 
            $formattedResults = @()
            if ($response.value) {
                foreach ($item in $response.value) {
                    $formattedObject = [PSCustomObject]@{}
    
                    foreach ($property in $item.PSObject.Properties) {
                        $originalName = $property.Name
                        $originalValue = $property.Value

                        # Capitalize the first letter of the property name
                        $capitalizedName = ($originalName[0].ToString().ToUpper()) + ($originalName.Substring(1).ToLower())
        
                        $formattedObject | Add-Member -MemberType NoteProperty -Name $capitalizedName -Value $originalValue
                    }

                    $formattedResults += $formattedObject
                }
            }

            return $formattedResults
        }
        catch {
            Write-Error "Failed to fetch Azure DevOps projects for organization '$Organization': $_"
        }
    }
}