Functions/Get-IAConfigurationDataModel.ps1

Function Get-IAConfigurationDataModel{
    <#
        .SYNOPSIS
            Returns a Data Model for Widget Configuration.
        .DESCRIPTION
            Configuration Data Models are used to set-up the Widget. They contain all of the different selections and variables nessessary for a Widget.
            A Configuration Data Model is just like the Data Source that you use in the IA website to configure a widget.
        .OUTPUTS
            The Configuraiton Data Model as a PSObject.
        .EXAMPLE
            Get-IAConfigurationDataModel -Interactive
             
    #>

    [CmdletBinding(DefaultParameterSetName='Name')]
    Param(
        [Parameter(ParameterSetName = 'Interactive')]
        [Switch]$Interactive
    )
    DynamicParam
    {
        try{
            # Name
            $Bucket = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
    
            $AttributeList = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            $Values = (([XML](Invoke-IAQuery -QueryUrl 'configmgr/$metadata' -Method Get)).edmx.DataServices.schema | 
                        Where-Object -Property Namespace -EQ IA_Backend.Entities.DataDefinitions.ConfigMgr).EntityType.Name
            $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)
            #Name

            # WidgetTemplateName

            $AttributeList = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            $Values = (Get-IAWidgetTemplate -All).Name
            $AttribValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($Values)
            $AttributeList.Add($AttribValidateSet)
    
            $AttribParameter = New-Object System.Management.Automation.ParameterAttribute
            $AttribParameter.Mandatory = $true
            $AttribParameter.ParameterSetName = 'WidgetTemplateName'
            $AttribParameter.Position = 1
            $AttributeList.Add($AttribParameter)
            $ParameterName = 'WidgetTemplateName'
            $Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($ParameterName, [String], $AttributeList)
            $Bucket.Add($ParameterName, $Parameter)
            return $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
            }
        }

        [XML]$XML = Invoke-IAQuery -QueryUrl 'configmgr/$metadata' -Method Get

        $XMLBase = $XML.edmx.DataServices.schema

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

        if($Interactive){

            $Name = $ConfigurationDataModels | Select-Object -ExpandProperty Name | Out-GridView -PassThru
        }
        elseif($WidgetTemplateName){

            $WidgetTemplate = Get-IAWidgetTemplate -Name $WidgetTemplateName

            $ConnectorModule = Get-IAConnectorModule -All | Where-Object -Property ConnectorModuleTypeId -eq $WidgetTemplate.ConnectorModuleTypeId

            if($LicenseId) { $ConnectorModule = $ConnectorModule | Where-Object -Property ConnectorId -eq $LicenseId }

            if($ConnectorModule.Count -gt 1) { $ConnectorModule = $ConnectorModule | Select-Object -First 1 }

            $Name = $ConnectorModule.Name + 'Configuration'
        }

        $DataModel = $ConfigurationDataModels | Where-Object -Property Name -Eq $Name

        $CompleteDataModel = New-Object -TypeName psobject

        foreach($property in $DataModel.Property){

            $CompleteDataModel | Add-Member -MemberType NoteProperty -Name $property.Name -Value ""
        }

        $ReturnObject = New-Object -TypeName PSObject
        $ReturnObject | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Name
        $ReturnObject | Add-Member -MemberType NoteProperty -Name 'DataModel' -Value $CompleteDataModel

        return $ReturnObject
    }
}