Public/Get-DuneUpdate.ps1

<#
.SYNOPSIS
Retrieve pending or available updates.

.DESCRIPTION
Gets update objects (patching updates). Supports filtering by `Name`, `Id`, `Resource` (pipeline input), and severity. Use `-Raw` for raw API output.

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

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

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

.PARAMETER Severity
Filter updates by severity.

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

.EXAMPLE
PS> Get-DuneUpdate
Returns known updates.

.EXAMPLE
PS> Get-DuneUpdate -Id 3d8f6b5a-...
Returns the update with the specified `Id`.

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

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

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

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

        [Parameter()]
        [string]$Severity,

        [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) {
            'Id' { 'patching/updates?Id={0}' -f $Id }
            'Resource' { 'resources/{0}/pendingupdates' -f $Resource.Id }
            Default { "patching/updates" }
        }

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

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        # ApiFilters won't work for resources/{0}/pendingupdates endpoint
        if ($Name) { $ReturnObjects = $ReturnObjects | Where-Object Name -Like $Name }
        if ($Severity) { $ReturnObjects = $ReturnObjects | Where-Object Severity -Like $Severity }
        return $ReturnObjects
    }
}