Functions/Get-IAWidget.ps1

Function Get-IAWidget {
    <#
        .SYNOPSIS
            Get a list of Widgets.
        .DESCRIPTION
            This function is used to get data around Widgets, -Expand can also be used to include related parts to the Widget.
            Use -IncludeConfiguration inline with -Expand to have the Configuration of the Widget added to the response.
            Note: -IncludeConfiguration can not be used with -All, it has to be directed towards a single Widget.
        .EXAMPLE
            Get-IAWidget -All
        .EXAMPLE
            Get-IAWidget -All -Expand
    #>

    Param(
        [Parameter(Mandatory = $true, ParameterSetName='Name')]
        [String] $Name,
        [Parameter(Mandatory = $true, ParameterSetName='Filter')]
        [String] $Filter,
        [Parameter(Mandatory = $true, ParameterSetName='All')]
        [Switch] $All,
        [Parameter(Mandatory = $true, ParameterSetName='Id')]
        [GUID] $Id,
        [Parameter(ParameterSetName='Expand')]
        [Parameter(ParameterSetName='Id')]
        [Parameter(ParameterSetName='All')]
        [Parameter(ParameterSetName='Name')]
        [Switch] $Expand,
        [Parameter(ParameterSetName='Expand')]
        [Parameter(ParameterSetName='Id')]
        [Parameter(ParameterSetName='Name')]
        [Switch] $IncludeConfiguration
    )

    $Uri = "Widgets"

    if($All){}
    elseif($Id){
        $Uri += "($Id)"
    }
    elseif($Name){
        $Uri += "?&`$filter=Title eq '$Name'"
    }
    elseif ($Filter) {
        $Uri += "?&`$filter=$Filter"
    }

    if($Expand){

        $Uri += '?$Expand=*'
    }

    $response = Invoke-IAQuery -QueryUrl $Uri -Method Get
    
    if(!$IncludeConfiguration){
        if ($null -eq $response.value -and !$Expand -and !$Id) {
            return $null
        }
        elseif($Expand -or $Id){

            if($All){ return $response.value }
            else { return $response }
        }
        else{
            return $response.value
        }
    }
    else{
        if($Expand -and $IncludeConfiguration){
            
            try{
                $ConfigurationId = $Response.DataDefinition.ConfigurationId
                $ConfigurationName = $Response.ConnectorModuleType.Name + "Configurations"
                $Configuration = Get-IAConfiguration -Name $ConfigurationName -Id $ConfigurationId 

                $Response | Add-Member -MemberType NoteProperty -Name 'Configuration' -Value $Configuration

                return $response
            }
            catch{
                $CurrentError = $PSItem
                Write-Verbose $CurrentError
                return $response
            }
        }
    }
}