functions/secret-permissions/Update-SecretPermission.ps1

function Update-SecretPermission {
    <#
    .SYNOPSIS
    Update a Secret Permission
 
    .DESCRIPTION
    Update a Secret Permission
 
    .EXAMPLE
    $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
    Set-TssSecretPermission -TssSession $session -Id 242
 
    Update Secret Permission 242, setting access role to View
 
    .LINK
    https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-permissions/Set-TssSecretPermission
 
    .LINK
    https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-permission/Set-SecretPermission.ps1
 
    .NOTES
    Requires TssSession object returned by New-TssSession
    #>

    [cmdletbinding(SupportsShouldProcess)]
    [OutputType('TssSecretPermission')]
    param(
        # TssSession object created by New-TssSession for auth
        [Parameter(Mandatory, ValueFromPipeline, Position = 0)]
        [TssSession]
        $TssSession,

        # Secret Permission ID
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('SecretPermissionId')]
        [int[]]
        $Id,

        # Secret Permission ID
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [int]
        $SecretId,

        # Granted Role name
        [Parameter(Mandatory)]
        [ValidateSet('List', 'View', 'Edit', 'Owner')]
        [string]
        $AccessRole
    )
    begin {
        $setParams = $PSBoundParameters
        $invokeParams = . $GetInvokeTssParams $TssSession
    }
    process {
        Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
        if ($setParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
            . $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation
            foreach ($secretP in $Id) {
                $restResponse = $null
                $uri = $TssSession.ApiUrl, 'secret-permissions', $secretP -join '/'
                $invokeParams.Uri = $uri
                $invokeParams.Method = 'PUT'

                $updateBody = @{
                    id = $secretP
                    secretId = $SecretId
                    secretAccessRoleName = $AccessRole
                }

                $invokeParams.Body = $updateBody | ConvertTo-Json

                if ($PSCmdlet.ShouldProcess("Secret Permission ID: $secretP", "$($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n")) {
                    Write-Verbose "Performing the operation $($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n"
                    try {
                        $restResponse = . $InvokeApi @invokeParams
                    } catch {
                        Write-Warning 'Issue updating secret permission [$secretP]'
                        $err = $_
                        . $ErrorHandling $err
                    }
                }
                if ($restResponse) {
                    [TssSecretPermission]$restResponse
                }
            }
        } else {
            Write-Warning 'No valid session found'
        }
    }
}