Functions/Get-IAFilter.ps1

Function Get-IAFilter {
    <#
        .SYNOPSIS
            Is used to query for different filters.
        .DESCRIPTION
            This function is used to get different filters that are used in Insight Analytics. Filters include Collections, Packages, TaskSequences and everything else that is used to configure and set-up Widgets.
        .EXAMPLE
            Get-IAFilter -All
    #>

    [CmdletBinding(DefaultParameterSetName='Name')]
    Param(
        [Parameter(Mandatory = $true, ParameterSetName='All')]
        [Switch] $All,
        [Parameter(ParameterSetName='Filter')]
        [String] $Filter
    )
    DynamicParam
    {
        try {
            $Bucket = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
               
            Try{
                $connectorsAvailable = @{}
                $connectorsEnabled = Get-IAConnector -All -Filter 'Enabled eq true'

                foreach($con in $connectorsEnabled){
                    $connectorTypes = Get-IAConnectorTypes -Filter "Id eq $($con.ConnectorTypeId)"
                    $assemblyName = $connectorTypes.AssemblyName
                    $NamespaceName = $assemblyName.Split(".")[-1]

                    $currentConfigurations = $null

                    if(![String]::IsNullOrEmpty($connectorTypes.ServiceRoute)){
                        $currentConfigurations = (Invoke-IAQuery -QueryUrl "$($connectorTypes.ServiceRoute)/" -Method Get)

                        $Values += $currentConfigurations.Value.Name
                        $properties = @{
                            "ServiceRoute" = $connectorTypes.ServiceRoute
                            "NamespaceName" = $NamespaceName
                            "Configurations" = $currentConfigurations
                        }

                        $connectorsAvailable.Add($con.Name,$properties)
                    }
                }
            }
            Catch{
                $CurrentError = $_
                throw [System.ArgumentException]::New('Failed to find the connector query urls!', $currentError.Exception) 
            }

            $AttributeList = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            $AttribValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($Values)
            $AttributeList.Add($AttribValidateSet)
    
            $AttribParameter = New-Object System.Management.Automation.ParameterAttribute
            $AttribParameter.Mandatory = $true
            $AttribParameter.ParameterSetName = 'Name'
            $AttribParameter.Position = 0
            $AttributeList.Add($AttribParameter)
            $ParameterName = 'Name'
            $Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($ParameterName,[String], $AttributeList)
            $Bucket.Add($ParameterName, $Parameter)
            $Bucket
        }
        catch [System.Net.WebException] {
            $CurrentError = $_
            throw [System.Net.WebException]::New('Make sure TLS1.2 is allowed!', $currentError.Exception)
        }
        catch {
            $CurrentError = $_

            if($CurrentError.Exception.Message -like '*https:///api/Categories*'){
                throw [System.ArgumentException]::New('Make sure that you are connected to the environment!', $currentError.Exception)
            }
        }
    }
    End
    {
        Foreach ($key in $PSBoundParameters.Keys)
        {
            if ($MyInvocation.MyCommand.Parameters.$key.isDynamic)
            {
                Set-Variable -Name $key -Value $PSBoundParameters.$key
            }
        }

        Foreach($connector in $connectorsAvailable.Keys){
            if($connectorsAvailable.Item($connector).Configurations.Value.Name.Contains($Name)) { break }
        }

        $Uri = "$($connectorsAvailable.Item($connector).ServiceRoute)/"

        if($All){}
        elseif($Name){
            $Uri += "$Name"
        }

        if ($Filter) {
            $Uri += "?&`$filter=$Filter"
        }

        $response = Invoke-IAQuery -QueryUrl $Uri -Method Get
    
        if ($null -eq $response.value) {
            return $null
        }

        if($All){
            return $response.Value | Select-Object -ExpandProperty Name
        }
        else{
            return $response.value
        }
    }
}