Functions/Get-IAConfiguration.ps1

Function Get-IAConfiguration {
    <#
        .SYNOPSIS
            Returns a configuration that is used to set up a Widget
        .DESCRIPTION
            Each Widget requires a Configuration when created. This function can be used to get the Configuration, that is used when creating a Widget.
        .OUTPUTS
            An IA Configuration as a PSObject
        .EXAMPLE
            Get-IAConfiguration -Interactive
        .EXAMPLE
            Get-IAConfiguration -All
        .EXAMPLE
            Get-IAConfiguration -ConfigurationName Collections
        .EXAMPLE
            Get-IAConfiguration -ConfigurationName Collections -ConfigurationId 'ab273c53-1444-469d-6837-08d76e94133f'
             
    #>

    [CmdletBinding(DefaultParameterSetName='ConfigurationId')]
    Param(
        [Parameter(ParameterSetName='ConfigurationId')]
        [Guid] $Id,
        [Parameter(ParameterSetName='All')]
        [Switch]$All,
        [Parameter(Mandatory = $true, ParameterSetName='Interactive')]
        [Switch]$Interactive
    )
    DynamicParam
    {
        try{
            $Bucket = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
            $AttributeList = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            
            # Find which connectors are available
            Try{
                $connectorsAvailable = @{}
                $connectorsEnabled = Get-IAConnector -All -ExpandSpecific 'ConnectorType' -Filter 'Enabled eq true'

                foreach($con in $connectorsEnabled){
                    $connectorType = $con.ConnectorType
                    $assemblyName = $connectorType.AssemblyName
                    $NamespaceName = $assemblyName.Split(".")[-1]

                    $properties = @{
                        "ServiceRoute" = $connectorType.ServiceRoute
                        "NamespaceName" = $NamespaceName
                        "ConnectorTypeId" = $connectorType.Id
                    }

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

            foreach($item in $connectorsAvailable.Keys){
               $Values += (Invoke-IAQuery -QueryUrl "$($connectorsAvailable.Item($item).ServiceRoute)/" -Method Get).Value.Name
            }

            $AttribValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($Values)
            $AttributeList.Add($AttribValidateSet)
    
            $AttribParameter = New-Object System.Management.Automation.ParameterAttribute
            $AttribParameter.Mandatory = $true
            $AttribParameter.ParameterSetName = 'ConfigurationId'
            $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($item in $connectorsAvailable.Keys){
            $Values = $null
            if($Name){
                $Values = (Invoke-IAQuery -QueryUrl "$($connectorsAvailable.Item($item).ServiceRoute)/" -Method Get).Value.Name
            }

            if($Values -ne $null){
                if($Name -ne $null-and $Values.Contains($Name)){
                    break;
                }
            }
        }

        if($Interactive){

            [XML]$XML = Invoke-IAQuery -QueryUrl "$($connectorsAvailable.Item($item).ServiceRoute)/$metadata" -Method Get
            $XMLBase = $XML.edmx.DataServices.schema

            $ConfigurationDataModels = $XMLBase | 
                Where-Object -Property Namespace -EQ IA_Backend.Entities.DataDefinitions.ConfigMgr | 
                Select-Object -ExpandProperty EntityType

            $Name = $ConfigurationDataModels | Select-Object -ExpandProperty Name | Out-GridView -PassThru
            $Name = $Name + "s"
        }

        $Uri = "$($connectorsAvailable.Item($item).ServiceRoute)/$Name"

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

        $response = Invoke-IAQuery -QueryUrl $Uri -Method Get

        if($Interactive -or $All -or ($Name -and !$Id))
        {
            return $response.value
        }
        else{

            return $response
        }
    }
}