Public/Get-DuneService.ps1

<#
.SYNOPSIS
Retrieve resource addon services.

.DESCRIPTION
Gets service entries for resource addons. Supports filtering by name, id, related resource, display name, state, and raw output option. Returns `DuneService` objects by default.

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

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

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

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

.PARAMETER State
Filter services by state (supports wildcard match).

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

.EXAMPLE
PS> Get-DuneService -Name "iis"
Returns services with names matching `iis`.

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

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

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

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

        [Parameter()]
        [string]$DisplayName,

        [Parameter()]
        [string]$State,

        [Parameter()]
        [switch]$Raw
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $ReturnObjects = @()
        $ProcessedUrls = @()
        $BaseUri = "resourceaddons/services"
        $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
        }
        if ($State) {
            $Uri = $Uri | Add-UriQueryParam "StateILike=$State" -ConvertWildcards
        }

        # ApiCall and Object conversion
        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 DuneService -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
    }
}