Scripts/Remove-BPASystemPermission.ps1

function Remove-BPASystemPermission {
    <#
        .SYNOPSIS
            Removes AutoMate BPA system permissions.
 
        .DESCRIPTION
            Remove-BPASystemPermission removes the provided system permissions.
 
        .PARAMETER InputObject
            The permissions object(s) to remove.
 
        .INPUTS
            SystemPermission objects are deleted by this function.
 
        .EXAMPLE
            # Remove system permissions for user "MyUsername"
            Get-BPAUser "MyUsername" | Get-BPASystemPermission | Remove-BPASystemPermission
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 09/19/2016
            Date Modified : 02/08/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    param(
        [Parameter(Position = 0, ParameterSetName = "ByPipeline", ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        $InputObject
    )

    PROCESS {       
        foreach ($obj in $InputObject) {
            switch ($obj.TypeName) {
                "SystemPermission" {                    
                    $affectedUser = Get-BPAUserGroup -ID $obj.GroupID
                    if ($null -eq $affectedUser) {
                        $affectedUser = Get-BPAUser -ID $obj.GroupID
                    }
                    if ($PSCmdlet.ShouldProcess($obj.BPAServer, "Removing $($obj.TypeName): $($affectedUser.Name)")) {
                        Write-Verbose "Removing system permission for '$($affectedUser.Name) (Type: $($affectedUser.TypeName))'."
                        Invoke-BPARestMethod -Resource "system_permissions/$($obj.ID)/delete" -RestMethod Post | Out-Null
                    }
                }
                default {
                    Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
                }
            }
        }
    }
}