functions/secrets/Set-TssSecretSecurityMfa.ps1

function Set-TssSecretSecurityMfa {
    <#
    .SYNOPSIS
    Turn multifactor authentication on or off for a secret
 
    .DESCRIPTION
    Sets whether viewing the secret requires multifactor authentication.
    Returns the security settings of the secret.
 
    .PARAMETER TssSession
    TssSession object created by New-TssSession for authentication
 
    .PARAMETER Id
    Secret ID
 
    .PARAMETER AutoCheckIn
    Check the secret back in after the request completes
 
    .PARAMETER AutoCheckout
    Check out the secret automatically when it requires checkout
 
    .PARAMETER AutoComment
    Comment to record with the automatic checkout, for secrets that require a comment
 
    .PARAMETER ForceCheckIn
    Force check in the secret if another user has it checked out
 
    .PARAMETER RequireMultifactorAuthentication
    Require multifactor authentication to view the secret. Use -RequireMultifactorAuthentication:$false to turn it off
 
    .EXAMPLE
    $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
    Set-TssSecretSecurityMfa -TssSession $session -Id 42 -RequireMultifactorAuthentication
 
    Requires multifactor authentication to view secret 42
 
    .EXAMPLE
    $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
    Set-TssSecretSecurityMfa -TssSession $session -Id 42 -RequireMultifactorAuthentication:$false
 
    Stops requiring multifactor authentication on secret 42
 
    .LINK
    https://thycotic-ps.github.io/thycotic.secretserver/commands/secrets/Set-TssSecretSecurityMfa
 
    .LINK
    https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secrets/Set-TssSecretSecurityMfa.ps1
 
    .NOTES
    Requires TssSession object returned by New-TssSession
 
    Requires Secret Server Cloud with Platform integration and a Standard or higher license; Secret Server rejects the change otherwise.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('Thycotic.PowerShell.Secrets.Security')]
    param (
        [Parameter(Mandatory, ValueFromPipeline, Position = 0)]
        [Thycotic.PowerShell.Authentication.Session]
        $TssSession,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('SecretId')]
        [int]
        $Id,

        [switch]
        $AutoCheckIn,

        [switch]
        $AutoCheckout,

        [string]
        $AutoComment,

        [switch]
        $ForceCheckIn,

        [switch]
        $RequireMultifactorAuthentication
    )
    begin {
        $tssParams = $PSBoundParameters
        $invokeParams = . $GetInvokeApiParams $TssSession
    }
    process {
        Get-TssInvocation $PSCmdlet.MyInvocation
        if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
            Compare-TssVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation
            $uri = $TssSession.ApiUrl, 'secrets', $Id, 'security-multifactor-authentication' -join '/'
            $queryParams = @()
            switch ($tssParams.Keys) {
                'AutoCheckIn' {
                    $queryParams += "autoCheckIn=$([boolean]$AutoCheckIn)"
                }
                'AutoCheckout' {
                    $queryParams += "autoCheckout=$([boolean]$AutoCheckout)"
                }
                'AutoComment' {
                    $queryParams += "autoComment=$([uri]::EscapeDataString($AutoComment))"
                }
                'ForceCheckIn' {
                    $queryParams += "forceCheckIn=$([boolean]$ForceCheckIn)"
                }
            }
            if ($queryParams) {
                $uri = $uri, ($queryParams -join '&') -join '?'
            }
            $invokeParams.Uri = $uri
            $invokeParams.Method = 'PATCH'
            $body = @{ data = @{ } }
            switch ($tssParams.Keys) {
                'RequireMultifactorAuthentication' {
                    $body.data.Add('isMultifactorAuthenticationRequired', @{ dirty = $true; value = [boolean]$RequireMultifactorAuthentication })
                }
            }
            $invokeParams.Body = $body | ConvertTo-Json -Depth 100
            if ($PSCmdlet.ShouldProcess("Secret security mfa: $Id", "$($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n")) {
                Write-Verbose "Performing the operation $($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n"
                try {
                    $apiResponse = Invoke-TssApi @invokeParams
                    $restResponse = . $ProcessResponse $apiResponse
                } catch {
                    Write-Warning 'Issue with Set-TssSecretSecurityMfa request'
                    $err = $_
                    . $ErrorHandling $err
                }
                if ($restResponse) {
                    [Thycotic.PowerShell.Secrets.Security](. $FilterTssResponse $restResponse ([Thycotic.PowerShell.Secrets.Security]))
                }
            }
        } else {
            Write-Warning 'No valid session found'
        }
    }
}