Functions/Get-IAWidgetTemplate.ps1

Function Get-IAWidgetTemplate {
    <#
        .SYNOPSIS
            Returns a Widget Template.
        .DESCRIPTION
            Widget Templates show all of the different Widgets that are available to be created. 'Boot Time', 'BSOD List', 'Software Update' for example.
        .EXAMPLE
            Get-IAWidgetTemplate -All
    #>

    [CmdletBinding(DefaultParameterSetName='Name')]
    Param(
        [Parameter(Mandatory = $true, ParameterSetName='Filter')]
        [String] $Filter,
        [Parameter(Mandatory = $true, ParameterSetName='All')]
        [Switch] $All,
        [Parameter(Mandatory = $true, ParameterSetName='Id')]
        [Guid] $Id,
        [Parameter(ParameterSetName='Id')]
        [Switch] $Expand,
        [Parameter(ParameterSetName='Id')]
        [Parameter(ParameterSetName='All')]
        [String] $ExpandSpecific
    )
    DynamicParam
    {
        try{
            $Name = $null
            $Bucket = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
    
            $AttributeList = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            $Values = (Invoke-IAQuery -QueryUrl 'WidgetTemplates' -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 = '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
            }
        }
        
        $Uri = "WidgetTemplates"

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

        if($ExpandSpecific -and $Filter){
         $Uri += '?$Expand=' + $ExpandSpecific + "&?`$filter=$Filter"
        }
        elseif($ExpandSpecific -and $All){ $Uri += "?`$Expand=$ExpandSpecific" }
        elseif ($ExpandSpecific -and $Id) { $Uri += '?$Expand=' + $ExpandSpecific }
        elseif ($ExpandSpecific -and $Name) { $Uri += "&?`$Expand=$ExpandSpecific" }
        elseif($Filter) { $Uri += "?`$filter=$Filter" }
        elseif($Expand) { $Uri += '?$Expand=*'}

        $response = Invoke-IAQuery -QueryUrl $Uri -Method Get
        
        if($Id){
            
            return $response
        }
        else{

            if ($null -eq $response.value) {
                return $null
            }

            return $response.value
        }
    }
}