Functions/Get-IAGroup.ps1

Function Get-IAGroup {
    <#
        .SYNOPSIS
            Gets Insight Analytics Groups
        .DESCRIPTION
            This function can be used to get the list of all different Groups in Insight Analytics.
        .OUTPUTS
            An IA Group as a PSObject
        .EXAMPLE
            Get-IAGroup -All
        .EXAMPLE
            Get-IAGroup -All -Expand
        .EXAMPLE
            Get-IAGroup -Id '30b57610-5913-4a48-afb0-5e0be987aec6' -Expand
        .EXAMPLE
            Get-IAGroup -Name 'Management Group' -Expand
    #>

    [CmdletBinding(DefaultParameterSetName='Name')]
    Param(
        [Parameter(ParameterSetName='Filter', Mandatory = $true)]
        [String] $Filter,
        [Parameter(ParameterSetName='All')]
        [Switch] $All,
        [Parameter(ParameterSetName='All')]
        [Parameter(ParameterSetName='Id')]
        [Parameter(ParameterSetName='Name')]
        [Switch] $Expand,
        [Parameter(ParameterSetName='Id', Mandatory = $true)]
        [String] $Id
    )
    DynamicParam
    {
        $Bucket = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
    
        $AttributeList = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
        $Values = (Invoke-IAQuery -QueryUrl "Groups" -Method Get).Value.Title
        $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
    }
    End
    {
        Foreach ($key in $PSBoundParameters.Keys)
        {
            if ($MyInvocation.MyCommand.Parameters.$key.isDynamic)
            {
                Set-Variable -Name $key -Value $PSBoundParameters.$key
            }
        }

        $Uri = "Groups"

        if($Id){

            $Uri += "($Id)"
        }

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

        if($Expand){

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

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

        if(!$Id) {

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

            return $response.value
        }
        else{

            return $response
        }
    }
}