public/Remove-VPASAccountFromAccountGroup.ps1

<#
.Synopsis
   DELETE ACCOUNT FROM ACCOUNT GROUP
   CREATED BY: Vadim Melamed, EMAIL: vpasmodule@gmail.com
.DESCRIPTION
   USE THIS FUNCTION TO DELETE ACCOUNT FROM ACCOUNT GROUP
.LINK
   https://vpasmodule.com/commands/Remove-VPASAccountFromAccountGroup
.PARAMETER token
   HashTable of data containing various pieces of login information (PVWA, LoginToken, HeaderType, etc).
   If -token is not passed, function will use last known hashtable generated by New-VPASToken
.PARAMETER safe
   Safe name that will be used to query for the target account if no AcctID is passed
.PARAMETER username
   Username that will be used to query for the target account if no AcctID is passed
.PARAMETER platform
   PlatformID that will be used to query for the target account if no AcctID is passed
.PARAMETER address
   Address that will be used to query for the target account if no AcctID is passed
.PARAMETER AcctID
   Unique ID that maps to a single account, passing this variable will skip any query functions
.PARAMETER GroupName
   Unique target GroupName that will be used to query for the GroupID if no GroupID is passed
   An account group is set of accounts that will have the same password synced across the entire group
.PARAMETER GroupID
   Unique ID that maps to the target AccountGroup
   Supply GroupID to skip any querying for target AccountGroup
.PARAMETER WhatIf
   Run code simulation to see what is affected by running the command as well as any possible implications
   This is a code simulation flag, meaning the command will NOT actually run
.PARAMETER HideWhatIfOutput
   Suppress any code simulation output from the console
.PARAMETER InputParameters
   HashTable of values containing the parameters required to make the API call
.EXAMPLE
   $WhatIfSimulation = Remove-VPASAccountFromAccountGroup -GroupID {GROUPID VALUE} -AcctID {ACCTID VALUE} -WhatIf
.EXAMPLE
   $DeleteAccountFromAccountGroupStatus = Remove-VPASAccountFromAccountGroup -GroupID {GROUPID VALUE} -AcctID {ACCTID VALUE}
.EXAMPLE
   $DeleteAccountFromAccountGroupStatus = Remove-VPASAccountFromAccountGroup -GroupName {GROUPNAME VALUE} -safe {SAFE VALUE} -platform {PLATFORM VALUE} -username {USERNAME VALUE} -address {ADDRESS VALUE}
.EXAMPLE
   $InputParameters = @{
        GroupID = "22_12"
        safe = "TargetSafeName"
        platform = "TargetPlatformID"
        username = "TargetUsername"
        address = "targetAddress"
        WhatIf = $true|$false
        HideWhatIfOutput = $true|$false
   }
   $DeleteAccountFromAccountGroupStatus = Remove-VPASAccountFromAccountGroup -InputParameters $InputParameters
.EXAMPLE
   $InputParameters = @{
        GroupID = "22_12"
        AcctID = "22_123"
        WhatIf = $true|$false
        HideWhatIfOutput = $true|$false
   }
   $DeleteAccountFromAccountGroupStatus = Remove-VPASAccountFromAccountGroup -InputParameters $InputParameters
.EXAMPLE
   $InputParameters = @{
        safe = "TargetSafeName"
        platform = "TargetPlatformID"
        username = "TargetUsername"
        address = "targetAddress"
        GroupName = "TargetAccountGroupName"
        WhatIf = $true|$false
        HideWhatIfOutput = $true|$false
   }
   $DeleteAccountFromAccountGroupStatus = Remove-VPASAccountFromAccountGroup -InputParameters $InputParameters
.EXAMPLE
   $InputParameters = @{
        AcctID = "22_123"
        GroupName = "TargetAccountGroupName"
        WhatIf = $true|$false
        HideWhatIfOutput = $true|$false
   }
   $DeleteAccountFromAccountGroupStatus = Remove-VPASAccountFromAccountGroup -InputParameters $InputParameters
.OUTPUTS
   $true if successful
   ---
   $false if failed
#>

function Remove-VPASAccountFromAccountGroup{
    [OutputType([bool],'System.Object')]
    [CmdletBinding(DefaultParameterSetName='Set1')]
    Param(

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Unique GroupID of the target account group (for example: 22_123)")]
        [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="Unique GroupID of the target account group (for example: 22_123)")]
        [String]$GroupID,

        [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set3',ValueFromPipelineByPropertyName=$true)]
        [String]$safe,

        [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set3',ValueFromPipelineByPropertyName=$true)]
        [String]$platform,

        [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set3',ValueFromPipelineByPropertyName=$true)]
        [String]$username,

        [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set3',ValueFromPipelineByPropertyName=$true)]
        [String]$address,

        [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="Unique AccountID of the target account (for example: 22_123)")]
        [Parameter(Mandatory=$true,ParameterSetName='Set4',ValueFromPipelineByPropertyName=$true,HelpMessage="Unique AccountID of the target account (for example: 22_123)")]
        [String]$AcctID,

        [Parameter(Mandatory=$true,ParameterSetName='Set3',ValueFromPipelineByPropertyName=$true,HelpMessage="Unique group name of the target account group (for example: VpasGroup01)")]
        [Parameter(Mandatory=$true,ParameterSetName='Set4',ValueFromPipelineByPropertyName=$true,HelpMessage="Unique group name of the target account group (for example: VpasGroup01)")]
        [String]$GroupName,

        [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set3',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set4',ValueFromPipelineByPropertyName=$true)]
        [Switch]$WhatIf,

        [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set3',ValueFromPipelineByPropertyName=$true)]
        [Parameter(Mandatory=$false,ParameterSetName='Set4',ValueFromPipelineByPropertyName=$true)]
        [Switch]$HideWhatIfOutput,

        [Parameter(Mandatory=$true,ParameterSetName='InputParameters',ValueFromPipelineByPropertyName=$true,HelpMessage="Hashtable of parameters required to make API call, refer to get-help -examples for valid inputs")]
        [hashtable]$InputParameters,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
        [hashtable]$token
    )

    Begin{
        $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain,$EnableTroubleshooting = Get-VPASSession -token $token
        $CommandName = $MyInvocation.MyCommand.Name
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND
    }
    Process{
        try{
            if($PSCmdlet.ParameterSetName -eq "InputParameters"){
                $KeyHash = @{
                    set1 = @{
                        AcceptableKeys = @("GroupID","safe","platform","username","address","WhatIf","HideWhatIfOutput")
                        MandatoryKeys = @("GroupID")
                    }
                    set2 = @{
                        AcceptableKeys = @("GroupID","AcctID","WhatIf","HideWhatIfOutput")
                        MandatoryKeys = @("GroupID","AcctID")
                    }
                    set3 = @{
                        AcceptableKeys = @("safe","platform","username","address","GroupName","WhatIf","HideWhatIfOutput")
                        MandatoryKeys = @("GroupName")
                    }
                    set4 = @{
                        AcceptableKeys = @("AcctID","GroupName","WhatIf","HideWhatIfOutput")
                        MandatoryKeys = @("AcctID","GroupName")
                    }
                }
                $CheckSet = Test-VPASHashtableKeysHelper -InputHash $InputParameters -KeyHash $KeyHash

                if(!$CheckSet){
                    $log = Write-VPASTextRecorder -inputval "FAILED TO FIND TARGET PARAMETER SET" -token $token -LogType MISC
                    Write-Verbose "FAILED TO FIND TARGET PARAMETER SET"
                    Write-VPASOutput -str "FAILED TO FIND TARGET PARAMETER SET...VIEW EXAMPLES BELOW:" -type E
                    $examples = Write-VPASExampleHelper -CommandName $CommandName
                    return $false
                }
                else{
                    foreach($key in $InputParameters.Keys){
                        Set-Variable -Name $key -Value $InputParameters.$key
                    }
                }
            }
        }catch{
            $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR
            $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC
            Write-Verbose "FAILED TO RETRIEVE ACCOUNT GROUP"
            Write-VPASOutput -str $_ -type E
            return $false
        }

        try{
            if([String]::IsNullOrEmpty($AcctID)){
                Write-Verbose "NO ACCTID SUPPLIED, INVOKING ACCTID HELPER"
                $AcctID = Get-VPASAccountIDHelper -token $token -safe $safe -platform $platform -username $username -address $address
            }
            else{
                Write-Verbose "ACCTID SUPPLIED, SKIPPING ACCOUNTID HELPER"
            }

            if([String]::IsNullOrEmpty($GroupID)){
                write-verbose "NO GROUPID PASSED, INVOKING GROUPID HELPER"
                if([String]::IsNullOrEmpty($safe) -or [String]::IsNullOrEmpty($GroupName)){
                    write-verbose "IF NO GROUPID IS PASSED, SAFE AND GROUPNAME MUST BE PASSED...RETURNING FALSE"
                    Write-VPASOutput -str "IF NO GROUPID IS PASSED, SAFE AND GROUPNAME MUST BE PASSED" -type E
                }
                else{
                    $GroupID = Get-VPASAccountGroupIDHelper -token $token -safe $safe -GroupName $GroupName
                }
            }
            else{
                Write-Verbose "GROUPID SUPPLIED...SKIPPING GROUPID HELPER"
            }
            if(!$GroupID){
                $log = Write-VPASTextRecorder -inputval "COULD NOT FIND UNIQUE GROUPID...RETURNING FALSE" -token $token -LogType MISC
                $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC
                write-verbose "COULD NOT FIND UNIQUE GROUPID...RETURNING FALSE"
                Write-VPASOutput -str "COULD NOT FIND UNIQUE GROUPID...RETURNING FALSE" -type E
                return $false
            }

            if($NoSSL){
                Write-Verbose "NO SSL ENABLED, USING HTTP INSTEAD OF HTTPS"
                $uri = "http://$PVWA/PasswordVault/API/AccountGroups/$GroupID/Members/$AcctID"
            }
            else{
                Write-Verbose "SSL ENABLED BY DEFAULT, USING HTTPS"
                $uri = "https://$PVWA/PasswordVault/API/AccountGroups/$GroupID/Members/$AcctID"
            }
            $log = Write-VPASTextRecorder -inputval $uri -token $token -LogType URI
            $log = Write-VPASTextRecorder -inputval "DELETE" -token $token -LogType METHOD

            if($WhatIf){
                $log = Write-VPASTextRecorder -token $token -LogType WHATIF1
                $WhatIfHash = @{}
                Write-Verbose "INITIATING COMMAND SIMULATION"

                $WhatIfInfo = Get-VPASAccountGroupMembers -GroupID $GroupID -token $token

                $AffectedAccountCounter = 0
                $AffectedAccounts = @()
                foreach($WhatIfRec in $WhatIfInfo){

                    $WhatIfAccountID = $WhatIfRec.AccountID
                    $WhatIfSafeName = $WhatIfRec.SafeName
                    $WhatIfPlatformID = $WhatIfRec.PlatformID
                    $WhatIfAddress = $WhatIfRec.Address
                    $WhatIfUserName = $WhatIfRec.UserName

                    if($WhatIfAccountID -eq $AcctID){
                        $WhatIfTargetAccountID = $WhatIfAccountID
                        $WhatIfTargetSafeName = $WhatIfSafeName
                        $WhatIfTargetPlatformID = $WhatIfPlatformID
                        $WhatIfTargetAddress = $WhatIfAddress
                        $WhatIfTargetUserName = $WhatIfUserName
                    }
                    else{
                        $AffectedAccountCounter += 1
                        $miniHash = @{
                            AccountID = $WhatIfAccountID
                            SafeName = $WhatIfSafeName
                            PlatformID = $WhatIfPlatformID
                            Address = $WhatIfAddress
                            UserName = $WhatIfUserName
                        }
                        $AffectedAccounts += $miniHash
                    }
                }
                if([String]::IsNullOrEmpty($WhatIfTargetAccountID)){
                    write-verbose "COULD NOT FIND UNIQUE ACCTID...RETURNING FALSE"
                    Write-VPASOutput -str "COULD NOT FIND UNIQUE ACCTID...RETURNING FALSE" -type E
                    return $false
                }

                if(!$HideWhatIfOutput){
                    Write-VPASOutput -str "====== BEGIN COMMAND SIMULATION ======" -type S
                    Write-VPASOutput -str "THE FOLLOWING ACCOUNT WILL BE REMOVED FROM THE TARGET ACCOUNT GROUP:" -type S
                    Write-VPASOutput -str "AccountID : $WhatIfTargetAccountID" -type S
                    Write-VPASOutput -str "SafeName : $WhatIfTargetSafeName" -type S
                    Write-VPASOutput -str "PlatformID : $WhatIfTargetPlatformID" -type S
                    Write-VPASOutput -str "Address : $WhatIfTargetAddress" -type S
                    Write-VPASOutput -str "UserName : $WhatIfTargetUserName" -type S
                    Write-VPASOutput -str "RemainingAccountsInGroup : $AffectedAccounts" -type S
                    Write-VPASOutput -str "NumberOfRemainingAccountsInGroup : $AffectedAccountCounter" -type S
                    Write-VPASOutput -str "---" -type S
                    Write-VPASOutput -str "URI : $uri" -type S
                    Write-VPASOutput -str "METHOD : DELETE" -type S
                    Write-VPASOutput -str " " -type S
                    Write-VPASOutput -str "======= END COMMAND SIMULATION =======" -type S
                }

                $WhatIfHash = @{
                    WhatIf = @{
                        AccountID = $WhatIfTargetAccountID
                        SafeName = $WhatIfTargetSafeName
                        PlatformID = $WhatIfTargetPlatformID
                        Address = $WhatIfTargetAddress
                        UserName = $WhatIfTargetUserName
                        RemainingAccountsInGroup = $AffectedAccounts
                        NumberOfRemainingAccountsInGroup = $AffectedAccountCounter
                        RestURI = $uri
                        RestMethod = "DELETE"
                        Disclaimer = "THIS ACCOUNT WILL BE DELETED FROM THE ACCOUNT GROUP IF -WhatIf FLAG IS REMOVED"
                    }
                }

                $WhatIfJSON = $WhatIfHash | ConvertTo-Json | ConvertFrom-Json
                $log = Write-VPASTextRecorder -inputval $WhatIfJSON -token $token -LogType RETURNARRAY
                $log = Write-VPASTextRecorder -token $token -LogType WHATIF2
                return $WhatIfJSON
            }
            else{
                write-verbose "MAKING API CALL TO CYBERARK"

                if($sessionval){
                    $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method DELETE -ContentType "application/json" -WebSession $sessionval
                }
                else{
                    $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method DELETE -ContentType "application/json"
                }
                $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: TRUE" -token $token -LogType MISC
                Write-Verbose "RETURNING TRUE"
                return $true
            }
        }catch{
            $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR
            $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC
            Write-Verbose "UNABLE TO DELETE ACCTID: $AcctID FROM ACCOUNT GROUPID: $GroupID"
            Write-VPASOutput -str $_ -type E
            return $false
        }
    }
    End{
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER
    }
}