Scripts/Get-BPASystemPermission.ps1

function Get-BPASystemPermission {
    <#
        .SYNOPSIS
            Gets AutoMate BPA system permissions.
 
        .DESCRIPTION
            Get-BPASystemPermission gets system permissions.
 
        .PARAMETER InputObject
            The object(s) to retrieve permissions for.
 
        .PARAMETER FilterSet
            The parameters to filter the search on. Supply hashtable(s) with the following properties: Property, Comparator, Value.
            Valid values for the Comparator are: =, !=, <, >, contains (default - no need to supply Comparator when using 'contains')
 
        .PARAMETER FilterSetMode
            If multiple filter sets are provided, FilterSetMode determines if the filter sets should be evaluated with an AND or an OR
 
        .PARAMETER SortProperty
            The object property to sort results on. Do not use BPAServer or TypeName, since those are custom properties added by this module, and not exposed in the API.
 
        .PARAMETER SortDescending
            If specified, this will sort the output on the specified SortProperty in descending order. Otherwise, ascending order is assumed.
 
        .PARAMETER BPAServer
            The AutoMate BPA management server.
 
        .INPUTS
            Permissions for the following objects can be retrieved by this function:
            User
            UserGroup
 
        .EXAMPLE
            # Get permissions for user "MyUsername"
            Get-BPAUser "MyUsername" | Get-BPASystemPermission
 
        .EXAMPLE
            # Get permissions using filter sets
            Get-BPASystemPermission -FilterSet @{Property = 'EditDefaultPropertiesPermission'; Comparator = '='; Value = 'true'}
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 09/19/2016
            Date Modified : 03/15/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding(DefaultParameterSetName = "All")]
    [OutputType([System.Object[]])]
    param(
        [Parameter(Position = 0, ParameterSetName = "ByPipeline", ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        $InputObject,

        [Hashtable[]]$FilterSet,

        [ValidateSet("And","Or")]
        [string]$FilterSetMode = "And",

        [ValidateNotNullOrEmpty()]
        [string[]]$SortProperty = "GroupID",

        [switch]$SortDescending = $false,

        [ValidateNotNullOrEmpty()]
        [string]$BPAServer
    )

    BEGIN {
        $splat = @{
            RestMethod = "Get"
        }
        if ($BPAServer) {
            $splat += @{BPAServer = $BPAServer}
        }
        $result = @()
    }

    PROCESS {
        switch($PSCmdlet.ParameterSetName) {
            "All" {
                $splat += @{ Resource = Format-BPAUri -Path "system_permissions/list" -FilterSet $FilterSet -FilterSetMode $FilterSetMode -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() }
                $result = Invoke-BPARestMethod @splat
            }
            "ByPipeline" {
                foreach ($obj in $InputObject) {
                    Write-Verbose "Processing object Name: '$($obj.Name)' Type: '$($obj.TypeName)'"
                    $tempSplat = $splat
                    if (-not $tempSplat.ContainsKey("BPAServer")) {
                        $tempSplat += @{ BPAServer = $obj.BPAServer }
                    } else {
                        $tempSplat["BPAServer"] = $obj.BPAServer
                    }
                    switch ($obj.TypeName) {
                        {($_ -in @("User","UserGroup"))} {
                            $tempFilterSet = $FilterSet + @{Property = "GroupID"; Comparator = "="; Value = $obj.ID}
                            $tempSplat += @{ Resource = Format-BPAUri -Path "system_permissions/list" -FilterSet $tempFilterSet -FilterSetMode $FilterSetMode -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() }
                            $result += Invoke-BPARestMethod @tempSplat
                        }
                        default {
                            $unsupportedType = $obj.GetType().FullName
                            if ($_) { 
                                $unsupportedType = $_ 
                            } elseif (($null -ne $obj.Type) -and ($obj.Type -ne "")) {
                                $unsupportedType = $obj.Type
                            }
                            Write-Error -Message "Unsupported input type '$unsupportedType' encountered!" -TargetObject $obj
                        }
                    }
                }
            }
        }
    }

    END {
        $SortProperty += "BPAServer", "ID"
        return $result | Sort-Object $SortProperty -Unique -Descending:$SortDescending.ToBool()
    }
}