Public/Get-DuneGenericResource.ps1

function Get-DuneGenericResource {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param (
        [Parameter(Position = 0)]
        [string]$Name,

        [Parameter(ParameterSetName = "Id")]
        [guid]$Id,

        [Parameter(ParameterSetName = "Collection", ValueFromPipeline)]
        [DuneCollection]$Collection,

        [Parameter(ParameterSetName = "Deployment", ValueFromPipeline)]
        [DuneDeployment]$Deployment,

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

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

        [Parameter(ParameterSetName = "ResourceProvider", ValueFromPipeline)]
        [DuneResourceProvider]$ResourceProvider,

        [Parameter(ParameterSetName = "ExtId")]
        [string]$ExtId,

        [Parameter()]
        [string]$DisplayName,

        [Parameter()]
        [switch]$Raw,

        [Parameter()]
        [switch]$IncludeDeleted
    )

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

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

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { $BaseUri, $Id -join '/' }
            'ExtId' { '{0}?ExtIdIEquals={1}' -f $BaseUri, $ExtId }
            'Collection' { '{0}?CollectionId={1}' -f $BaseUri, $Collection.Id }
            'Deployment' { $BaseUri, "DeploymentId=$($Deployment.Id)" -join '?' }
            'ResourceGroup' { $BaseUri, "ParentId=$($ResourceGroup.Id)" -join '?' }
            'Resource' {
                if ($Resource.ObjectType -eq 'GenericResource') {
                    $BaseUri, $Resource.Id -join '/'
                }
                else {
                    Write-Warning "Wrong ObjectType $($Resource.ObjectType) for $($Resource.Name) (id: $($Resource.Id))"
                    return
                }
            }
            'ResourceProvider' { $BaseUri, "ResourceProviderId=$($ResourceProvider.Id)" -join '?' }
            Default { $BaseUri }
        }
        if ($Name) {
            $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
        }
        if ($DisplayName) {
            $Uri = $Uri | Add-UriQueryParam "DisplayNameILike=$DisplayName" -ConvertWildcards
        }
        if ($IncludeDeleted) {
            $Uri = $Uri | Add-UriQueryParam "IncludeDeleted=1"
        }
        $Uri = $Uri | Add-UriQueryParam "IncludeReferencedObjects=1"

        # ApiCall Cache
        if ($ProcessedUrls -notcontains $Uri) {
            try {
                $ResultItems = Invoke-DuneApiRequest -Uri $Uri -Method $Method -ExtractItems
                $ProcessedUrls += $Uri
                $ReturnObjects += $ResultItems | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneGenericResource -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        if ($ReturnObjects -and (-not $Raw) -and (-not $ReturnObjects.Deployment)) {
            $Deployments = if (($ReturnObjects.Count -eq 1) -and (-not $ReturnObjects.Deployment)) { $ReturnObjects | Get-DuneDeployment }else { Get-DuneDeployment }
            foreach ($Object in ($ReturnObjects | Where-Object { -not $_.Deployment })) {
                $Object.Deployment = $Deployments | Where-Object Id -EQ $Object.DeploymentId
            }
        }
        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object Name
    }
}