Public/Get-DuneAction.ps1

<#
.SYNOPSIS
Retrieve actions.
 
.DESCRIPTION
Gets action objects. Supports filtering by `Id`, `Name`, and configuration item context. Use `-Raw` to return raw API objects.
 
.PARAMETER Name
Filter actions by name (supports wildcards). Position 0.
 
.PARAMETER DisplayName
Filter by display name (supports wildcards).
 
.PARAMETER Id
The GUID of a specific action. Uses the `Id` parameter set.
 
.PARAMETER ActionAssignment
A `DuneActionAssignment` object; returns the action for the supplied assignment (pipeline input supported).
 
.PARAMETER Scope
Filter by action scope.
 
.PARAMETER IncludeReferencedObjects
If set, includes referenced objects in the response.
 
.PARAMETER Raw
If set, returns raw API objects instead of `DuneAction` objects.
 
.PARAMETER IncludeDeleted
Include deleted actions in results.
 
.EXAMPLE
PS> Get-DuneAction -Name "backup*"
Returns actions with names matching 'backup*'.
 
.EXAMPLE
PS> Get-DuneAction -Id 3d8f6b5a-...
Returns a specific action by GUID.
#>

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

        [Parameter()]
        [string]$DisplayName,

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

        [Parameter(ParameterSetName='ActionAssignment', ValueFromPipeline)]
        [DuneActionAssignment]$ActionAssignment,

        [Parameter()]
        [ActionScopes]$Scope,

        [Parameter()]
        [switch]$IncludeReferencedObjects,

        [Parameter()]
        [switch]$Raw,

        [Parameter()]
        [switch]$IncludeDeleted
    )

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

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

        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { "{0}/{1}" -f $BaseUri, $Id }
            'ActionAssignment' { "{0}/{1}" -f $BaseUri, $ActionAssignment.ActionId }
            'Name' { $BaseUri | Add-UriQueryParam 'N' }
            Default { $BaseUri }
        }

        if ($PSBoundParameters.ContainsKey('Name')) { $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards }
        if ($PSBoundParameters.ContainsKey('DisplayName')) { $Uri = $Uri | Add-UriQueryParam "DisplayNameILike=$DisplayName" -ConvertWildcards }
        if ($PSBoundParameters.ContainsKey('Scope')) { $Uri = $Uri | Add-UriQueryParam "Scope=$Scope" -ConvertWildcards }
        if ($IncludeReferencedObjects) { $Uri = $Uri | Add-UriQueryParam 'IncludeReferencedObjects=1' }
        if ($IncludeDeleted) { $Uri = $Uri | Add-UriQueryParam "IncludeDeleted=1" }

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

    end { return $ReturnObjects }
}