functions/users/Add-TssUserRole.ps1
|
function Add-TssUserRole { <# .SYNOPSIS Add roles to a user .DESCRIPTION Assigns one or more roles to a user. Roles the user already has are kept. .PARAMETER TssSession TssSession object created by New-TssSession for authentication .PARAMETER Id User ID .PARAMETER RoleId IDs of the roles to add .EXAMPLE $session = New-TssSession -SecretServer https://alpha -Credential $ssCred Add-TssUserRole -TssSession $session -Id 42 -RoleId 3, 5 Adds roles 3 and 5 to user 42 .LINK https://thycotic-ps.github.io/thycotic.secretserver/commands/users/Add-TssUserRole .LINK https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/users/Add-TssUserRole.ps1 .NOTES Requires TssSession object returned by New-TssSession #> [CmdletBinding(SupportsShouldProcess)] [OutputType('Thycotic.PowerShell.Users.ActionResult')] param ( [Parameter(Mandatory, ValueFromPipeline, Position = 0)] [Thycotic.PowerShell.Authentication.Session] $TssSession, [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [Alias('UserId')] [int] $Id, [Parameter(Mandatory)] [int[]] $RoleId ) 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, 'users', $Id, 'roles' -join '/' $invokeParams.Uri = $uri $invokeParams.Method = 'POST' $body = @{ } switch ($tssParams.Keys) { 'RoleId' { $body.Add('roleIds', $RoleId) } } $invokeParams.Body = $body | ConvertTo-Json -Depth 100 if ($PSCmdlet.ShouldProcess("User: $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 Add-TssUserRole request' $err = $_ . $ErrorHandling $err } if ($restResponse) { [Thycotic.PowerShell.Users.ActionResult](. $FilterTssResponse $restResponse ([Thycotic.PowerShell.Users.ActionResult])) } } } else { Write-Warning 'No valid session found' } } } |