Public/Get-DunePatchingWindow.ps1

<#
.SYNOPSIS
Retrieve patching windows.

.DESCRIPTION
Gets patching windows defined in Dune. Supports filtering by name, id, associated deployment, day of week, monthly occurrence and raw output option.

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

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

.PARAMETER Deployment
A `DuneDeployment` object; returns patching windows assigned to the provided deployment (pipeline input supported).

.PARAMETER DayOfWeek
Filter by day of week (exact match; wildcards not supported).

.PARAMETER MonthlyOccurence
Filter by monthly occurrence (exact match; wildcards not supported).

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

.EXAMPLE
PS> Get-DunePatchingWindow
Returns all patching windows.

.EXAMPLE
PS> Get-DunePatchingWindow -Id 3d8f6b5a-...
Returns the patching window with the specified `Id`.

.EXAMPLE
PS> Get-DuneDeployment -Name "MyApp" | Get-DunePatchingWindow
Pipeline example using the `Deployment` parameter set.
#>

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

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

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

        [Parameter()]
        [string]$DayOfWeek, #wildcard not supported

        [Parameter()]
        [string]$MonthlyOccurence, #wildcard not supported

        [Parameter()]
        [switch]$Raw
    )

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

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

        if ($Deployment) {
            $ReturnObjects += (Get-DunePatchingWindowAssignment -Deployment $Deployment).PatchingWindow
        }
        else {
            # Build Uri
            $Uri = switch ($PSCmdlet.ParameterSetName) {
                'Id' { '{0}?Id={1}' -f $BaseUri, $Id }
                Default { $BaseUri }
            }
            if ($Name) {
                $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
            }
            if ($DayOfWeek) {
                $Uri = $Uri | Add-UriQueryParam "DayOfWeek=$DayOfWeek"
            }
            if ($MonthlyOccurence) {
                $Uri = $Uri | Add-UriQueryParam "MonthlyOccurence=$MonthlyOccurence"
            }

            # 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 DunePatchingWindow -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
    }
}