Public/Get-DuneVariable.ps1

function Get-DuneVariable {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '',
        Justification = 'The parameter is indirectly used on ParameterSetName switch.')]
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline, ParameterSetName = "Deployment")]
        [DuneDeployment]$Deployment,

        [Parameter(ValueFromPipeline, ParameterSetName = "ResourceGroup")]
        [DuneResourceGroup]$ResourceGroup,

        [Parameter(ValueFromPipeline, ParameterSetName = "Resource")]
        [DuneResource]$Resource,

        [Parameter(ParameterSetName = "Tenant")]
        [switch]$Tenant,

        [Parameter()]
        [string]$Name,

        [Parameter()]
        [switch]$ExpandValue,

        [Parameter()]
        [Switch]$Raw
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $ReturnObjects = @()
        $ProcessedUrls = @()
        $Method = "GET"
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Deployment' { 'deployments/{0}/variables?IncludeInherited=true' -f $Deployment.Id }
            'ResourceGroup' { 'resourcegroups/{0}/variables?IncludeInherited=true' -f $ResourceGroup.Id }
            'Resource' { 'resources/{0}/variables?IncludeInherited=true' -f $Resource.Id }
            'Tenant' { 'tenants/variables?IncludeInherited=true' }
        }

        # ApiCall Cache
        if ($ProcessedUrls -notcontains $Uri) {
            try {
                # ApiCall and Object conversion
                $Response = Invoke-DuneApiRequest -Uri $Uri -Method $Method
                $ProcessedUrls += $Uri
                $Results = if ($Response.Content) { $Response.Content | ConvertFrom-Json }
                $ReturnObjects += $Results.items | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneVariable -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        if ($Name) {
            $ReturnObjects = $ReturnObjects | Where-Object Name -Like $Name
        }
        if ($ExpandValue) {
            $ReturnObjects.Value
        }
        else {
            return $ReturnObjects
        }
    }
}