public/Update-VPASEPVGroup.ps1

<#
.Synopsis
   UPDATE EPV GROUP
   CREATED BY: Vadim Melamed, EMAIL: vpasmodule@gmail.com
.DESCRIPTION
   USE THIS FUNCTION TO UPDATE AN EPV GROUP
.LINK
   https://vpasmodule.com/commands/Update-VPASEPVGroup
.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 NewGroupName
   New group name that the target EPV group will be updated with
.PARAMETER InputParameters
   HashTable of values containing the parameters required to make the API call
.EXAMPLE
   $UpdateEPVGroupJSON = Update-VPASEPVGroup -GroupName {GROUPNAME VALUE} -NewGroupName {NEWGROUPNAME VALUE}
.EXAMPLE
   $UpdateEPVGroupJSON = Update-VPASEPVGroup -GroupID {GROUPID VALUE} -NewGroupName {NEWGROUPNAME VALUE}
.EXAMPLE
   $InputParameters = @{
        GroupName = "TargetGroupName"
        NewGroupName = "NewGroupName"
   }
   $UpdateEPVGroupJSON = Update-VPASEPVGroup -InputParameters $InputParameters
.EXAMPLE
   $InputParameters = @{
        GroupID = "22"
        NewGroupName = "NewGroupName"
   }
   $UpdateEPVGroupJSON = Update-VPASEPVGroup -InputParameters $InputParameters
.OUTPUTS
   If successful:
   {
        "id": 244,
        "groupType": "Vault",
        "members": [
 
                    ],
        "groupName": "UpdatedGroupName",
        "description": "New group for documentation",
        "location": "\\"
   }
   ---
   $false if failed
#>

function Update-VPASEPVGroup{
    [OutputType('System.Object',[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="Enter new EPVGroup name to update target EPVGroup")]
        [Parameter(Mandatory=$true,ParameterSetName='Set2',ValueFromPipelineByPropertyName=$true,HelpMessage="Enter new EPVGroup name to update target EPVGroup")]
        [String]$NewGroupName,

        [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","NewGroupName")
                        MandatoryKeys = @("GroupName","NewGroupName")
                    }
                    set2 = @{
                        AcceptableKeys = @("GroupID","NewGroupName")
                        MandatoryKeys = @("GroupID","NewGroupName")
                    }
                }
                $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 EPV GROUP"
            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
            }

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

            $params = @{
                groupName = $NewGroupName
            }
            $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS
            $params = $params | ConvertTo-Json
            Write-Verbose "MAKING API CALL TO CYBERARK"

            if($sessionval){
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Body $params -Method PUT -ContentType "application/json" -WebSession $sessionval
            }
            else{
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Body $params -Method PUT -ContentType "application/json"
            }
            $outputlog = $response
            $log = Write-VPASTextRecorder -inputval $outputlog -token $token -LogType RETURN
            Write-Verbose "SUCCESSFULLY UPDATED EPVGROUP WITH NEW NAME: $NewGroupName"
            return $response

        }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 UPDATE EPVGROUP"
            Write-VPASOutput -str $_ -type E
            return $false
        }
    }
    End{
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER
    }
}