Skip to content

Commit

Permalink
Add-GroupMember - closes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed May 29, 2021
1 parent 4eb6a3f commit 10b2a3d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/functions/groups/Add-GroupMember.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function Add-GroupMember {
<#
.SYNOPSIS
Add a user to a group
.DESCRIPTION
Add a user to a group
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Set-TssNoun -TssSession $session -Id 8 -UserId 54
Add User ID 54 to Group ID 8
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/Set-TssNoun
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/<folder>/Set-Noun.ps1
.NOTES
Requires TssSession object returned by New-TssSession
#>
[cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = 'all')]
param(
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
[TssSession]
$TssSession,

# Group ID
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias('GroupId')]
[int]
$Id,

# User Ids to add to the Group
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[int[]]
$UserId
)
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.0000' $PSCmdlet.MyInvocation
foreach ($user in $UserId) {
$restResponse = $null
$uri = $TssSession.ApiUrl, 'groups', $Id, 'users' -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'POST'

$addBody = @{ userId = $user }
$invokeParams.Body = $addBody | ConvertTo-Json
if ($PSCmdlet.ShouldProcess("Group ID: $Id | User ID: $user", "$($invokeParams.Method) $uri with: `n$($invokeParams.Body)")) {
Write-Verbose "$($invokeParams.Method) $uri with: `n$($invokeParams.Body)"
try {
$restResponse = . $InvokeApi @invokeParams
} catch {
Write-Warning 'Issue removing [$]'
$err = $_
. $ErrorHandling $err
}

if ($restResponse) {
[TssGroupUser]$restResponse
}
}
}
} else {
Write-Warning 'No valid session found'
}
}
}
25 changes: 25 additions & 0 deletions tests/groups/Add-GroupMember.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1'))
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Id', 'UserId'
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')).Parameters.Keys
[object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru
}
Context "Verify parameters" -Foreach @{currentParams = $currentParams } {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters {
$_ | Should -BeNullOrEmpty
}
}
Context "Command specific details" {
It "$commandName should set OutputType to TssGroupUser" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'TssGroupUser'
}
}
}

0 comments on commit 10b2a3d

Please sign in to comment.