public/Add-VPASMemberEPVGroup.ps1

<#
.Synopsis
   ADD MEMBER TO EPV GROUP
   CREATED BY: Vadim Melamed, EMAIL: vpasmodule@gmail.com
.DESCRIPTION
   USE THIS FUNCTION TO ADD A MEMBER TO AN EPV GROUP
.LINK
   https://vpasmodule.com/commands/Add-VPASMemberEPVGroup
.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 GroupName
   Target EPV group name that a user will be added to
.PARAMETER GroupID
   Target EPV group ID that a user will be added to
.PARAMETER EPVUserName
   Target EPVUserName that will be added to target EPVGroup
.PARAMETER UserSearchIn
   Specify where to find the target EPVUser
   Possible values: Vault, Domain
.PARAMETER DomainDNS
   Specify the target directory mapping of the target EPVUser if the user is coming from a location of type Domain
.PARAMETER InputParameters
   HashTable of values containing the parameters required to make the API call
.EXAMPLE
   $AddMemberEPVGroupStatus = Add-VPASMemberEPVGroup -GroupName {GROUPNAME VALUE} -EPVUserName {USERNAME VALUE} -UserSearchIn domain -DomainDNS vman
.EXAMPLE
   $AddMemberEPVGroupStatus = Add-VPASMemberEPVGroup -GroupID {GROUPID VALUE} -EPVUserName {USERNAME VALUE} -UserSearchIn vault -DomainDNS vault
.EXAMPLE
   $InputParameters = @{
        GroupName = "TargetGroup1"
        EPVUserName = "TargetUser@vman.com
        UserSearchIn = "Vault"|"Domain"
        DomainDNS = "vman.com"|"Vault"
   }
   $AddMemberEPVGroupStatus = Add-VPASMemberEPVGroup -InputParameters $InputParameters
.EXAMPLE
   $InputParameters = @{
        GroupID = 22
        EPVUserName = "TargetUser@vman.com
        UserSearchIn = "Vault"|"Domain"
        DomainDNS = "vman.com"|"Vault"
   }
   $AddMemberEPVGroupStatus = Add-VPASMemberEPVGroup -InputParameters $InputParameters
.OUTPUTS
   $true if successful
   ---
   $false if failed
#>

function Add-VPASMemberEPVGroup{
    [OutputType([bool])]
    [CmdletBinding(DefaultParameterSetName='Set1')]
    Param(

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Perform search on target EPVGroup via GroupName")]
        [String]$GroupName,

        [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="Perform search on target EPVGroup via GroupID")]
        [String]$GroupID,

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Target EPVUser to be added to target EPVGroup")]
        [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="Target EPVUser to be added to target EPVGroup")]
        [String]$EPVUserName,

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Where to search for target EPVUser, either Vault or Domain")]
        [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="Where to search for target EPVUser, either Vault or Domain")]
        [ValidateSet('Vault','Domain')]
        [String]$UserSearchIn,

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="What directory mapping to find target EPVUser (for example: vman.com)")]
        [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="What directory mapping to find target EPVUser (for example: vman.com)")]
        [String]$DomainDNS,

        [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 = @("GroupName","EPVUserName","UserSearchIn","DomainDNS")
                        MandatoryKeys = @("GroupName","EPVUserName","UserSearchIn","DomainDNS")
                    }
                    set2 = @{
                        AcceptableKeys = @("GroupID","EPVUserName","UserSearchIn","DomainDNS")
                        MandatoryKeys = @("GroupID","EPVUserName","UserSearchIn","DomainDNS")
                    }
                }
                $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 ADD MEMBER TO EPVGROUP"
            Write-VPASOutput -str $_ -type E
            return $false
        }

        try{
            if($GroupID){
                write-verbose "SUPPLIED GROUPID, SKIPPING HELPER FUNCTION"
            }
            else{
                Write-Verbose "CONSTRUCTING SEARCH STRING TO QUERY CYBERARK"
                $searchQuery = "$GroupName"
                Write-Verbose "INVOKING HELPER FUNCTION TO RETRIEVE GROUPID"

                $GroupID = Get-VPASEPVGroupIDHelper -token $token -GroupName $GroupName
            }

            $params = @{}
            $params += @{memberId = $EPVUserName}
            write-verbose "ADDED EPVUSERNAME TO API PARAMS: Member = $EPVUserName"

            $params += @{memberType = $UserSearchIn}
            write-verbose "ADDED USERTYPE TO API PARAMS: MemberType = $UserSearchIn"

            if($UserSearchIn -eq "Domain"){
                $params += @{domainName = $DomainDNS}
                write-verbose "ADDED DOMAIN NAME TO API PARAMS: DomainName = $DomainDNS"
            }

            $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS
            $params = $params | ConvertTo-Json
            Write-Verbose "SUCCESSFULLY SETUP PARAMETERS FOR API CALL"

            if($NoSSL){
                Write-Verbose "NO SSL ENABLED, USING HTTP INSTEAD OF HTTPS"
                $uri = "http://$PVWA/PasswordVault/api/UserGroups/$GroupID/Members/"
            }
            else{
                Write-Verbose "SSL ENABLED BY DEFAULT, USING HTTPS"
                $uri = "https://$PVWA/PasswordVault/api/UserGroups/$GroupID/Members/"
            }

            Write-Verbose "MAKING API CALL TO CYBERARK"
            $log = Write-VPASTextRecorder -inputval $uri -token $token -LogType URI
            $log = Write-VPASTextRecorder -inputval "POST" -token $token -LogType METHOD

            if($sessionval){
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method POST -Body $params -ContentType "application/json" -WebSession $sessionval
            }
            else{
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method POST -Body $params -ContentType "application/json"
            }
            Write-Verbose "SUCCESSFULLY ADDED $EPVUserName TO $GroupID"
            $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: TRUE" -token $token -LogType MISC
            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 ADD $EPVUserName TO $GroupID"
            Write-VPASOutput -str $_ -type E
            return $false
        }
    }
    End{
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER
    }
}