Public/Get-DuneFact.ps1

<#
.SYNOPSIS
Retrieve Dune facts (resource addon facts).

.DESCRIPTION
Gets one or more facts associated with resources. You can filter by fact `Name`, `Id`, or provide a `DuneResource` object to get facts for that resource. Returns `DuneFact` objects by default; use `-Raw` for raw API output.

.PARAMETER Name
Filter facts by name (supports wildcards). Position 0 in the default parameter set.

.PARAMETER Id
The GUID of the fact. Use the `Id` parameter set to retrieve a specific fact.

.PARAMETER Resource
A `DuneResource` object; returns facts for the supplied resource (pipeline input supported).

.PARAMETER DisplayName
Filter facts by display name (supports wildcards).

.PARAMETER Raw
If set, returns raw API objects instead of `DuneFact` objects.

.EXAMPLE
PS> Get-DuneFact -Name "osversion"
Returns facts named `osversion`.

.EXAMPLE
PS> Get-DuneResource -Name "dbserver" | Get-DuneFact
Pipeline example using the `Resource` parameter set.
#>

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

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

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

        [Parameter()]
        [string]$DisplayName,

        [Parameter()]
        [switch]$Raw
    )

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

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

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { '{0}?Id={1}' -f $BaseUri, $Id }
            'Resource' { '{0}?ResourceId={1}' -f $BaseUri, $Resource.Id }
            Default { $BaseUri }
        }
        if ($Name) {
            $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
        }
        if ($DisplayName) {
            $Uri = $Uri | Add-UriQueryParam "DisplayNameILike=$DisplayName" -ConvertWildcards
        }

        # 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 DuneFact -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        return $ReturnObjects
    }
}