private/Get-VPASAccountRequestIDHelper.ps1

<#
.Synopsis
   GET ACCOUNT REQUEST ID
   CREATED BY: Vadim Melamed, EMAIL: vmelamed5@gmail.com
.DESCRIPTION
   HELPER FUNCTION TO RETRIEVE ACCOUNT REQUEST IDS FROM CYBERARK
#>

function Get-VPASAccountRequestIDHelper{
    [OutputType([String[]],[bool])]
    [CmdletBinding()]
    Param(

        [Parameter(ValueFromPipelineByPropertyName=$true,Position=0)]
        [String]$AcctID,

        [Parameter(ValueFromPipelineByPropertyName=$true,Position=1)]
        [String]$Safe,

        [Parameter(ValueFromPipelineByPropertyName=$true,Position=2)]
        [String]$Platform,

        [Parameter(ValueFromPipelineByPropertyName=$true,Position=3)]
        [String]$Username,

        [Parameter(ValueFromPipelineByPropertyName=$true,Position=4)]
        [String]$Address,

        [Parameter(ValueFromPipelineByPropertyName=$true,Position=5)]
        [String]$UserReason,

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

    Begin{
        $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain = Get-VPASSession -token $token
        $CommandName = $MyInvocation.MyCommand.Name
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND -Helper
    }
    Process{
        try{
            Write-Verbose "RETRIEVING ALL ACCOUNT REQUESTS"
            $FoundRequests = @()
            $AllAccountRequests = Get-VPASAllAccountRequests
            foreach($req in $AllAccountRequests.MyRequests){
                $curMatch = $true
                $requestID = $req.RequestID
                $requestAcctID = $req.AccountDetails.AccountID
                $requestReason = $req.UserReason
                $requestSafe = $req.AccountDetails.Properties.Safe
                $requestPlatform = $req.AccountDetails.Properties.PolicyID
                $requestAddress = $req.AccountDetails.Properties.Address
                $requestUsername = $req.AccountDetails.Properties.UserName

                if(![String]::IsNullOrEmpty($AcctID)){
                    if($requestAcctID -eq $AcctID){
                        if(![String]::IsNullOrEmpty($UserReason)){
                            if($requestReason -match $UserReason){
                                Write-Verbose "FOUND MATCHING REQUEST ID $requestID, ADDING TO RETURN LIST"
                                $FoundRequests += $requestID
                            }
                        }
                        else{
                            Write-Verbose "FOUND MATCHING REQUEST ID $requestID, ADDING TO RETURN LIST"
                            $FoundRequests += $requestID
                        }
                    }
                }
                else{
                    if(![String]::IsNullOrEmpty($Platform)){
                        if($requestPlatform -notmatch $Platform){
                            $curMatch = $false
                        }
                    }
                    if(![String]::IsNullOrEmpty($Safe)){
                        if($requestSafe -notmatch $Safe){
                            $curMatch = $false
                        }
                    }
                    if(![String]::IsNullOrEmpty($Address)){
                        if($requestAddress -notmatch $Address){
                            $curMatch = $false
                        }
                    }
                    if(![String]::IsNullOrEmpty($Username)){
                        if($requestUsername -notmatch $Username){
                            $curMatch = $false
                        }
                    }
                    if(![String]::IsNullOrEmpty($UserReason)){
                        if($requestReason -notmatch $UserReason){
                            $curMatch = $false
                        }
                    }
                    if($curMatch){
                        Write-Verbose "FOUND MATCHING REQUEST ID $requestID, ADDING TO RETURN LIST"
                        $FoundRequests += $requestID
                    }
                }
            }

            $counter = $FoundRequests.count
            if($counter -gt 0){
                Write-Verbose "FOUND TARGET ACCOUNT REQUESTS THAT MATCH THE SEARCHQUERY"
                $log = Write-VPASTextRecorder -inputval "FOUND TARGET ACCOUNT REQUESTS THAT MATCH THE SEARCHQUERY" -token $token -LogType MISC -Helper
                $log = Write-VPASTextRecorder -inputval $FoundRequests -token $token -LogType RETURN -Helper
                return $FoundRequests
            }
            else{
                Write-Verbose "NO ACCOUNT REQUESTS FOUND"
                Write-VPASOutput -str "NO ACCOUNT REQUESTS FOUND" -type E
                $log = Write-VPASTextRecorder -inputval "NO ACCOUNT REQUESTS FOUND" -token $token -LogType MISC -Helper
                $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: false" -token $token -LogType MISC -Helper
                return $false
            }
        }catch{
            Write-Verbose "UNABLE TO QUERY CYBERARK"
            Write-VPASOutput -str $_ -type E
            $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR -Helper
            $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC -Helper
        }
    }
    End{
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER -Helper
    }
}