From 02a10f430afdd5b62429a91705b6a406eaaa23d9 Mon Sep 17 00:00:00 2001 From: Shawn Melton <11204251+wsmelton@users.noreply.github.com> Date: Wed, 15 Sep 2021 06:34:11 -0500 Subject: [PATCH] Search-TssDirectoryServiceGroupMember - new command searching for member of Directory Service Group --- .../Search-TssDirectoryServiceGroupMember.md | 89 +++++++++++++++++++ .../classes/directory-services/GroupMember.cs | 12 +++ .../Search-TssDirectoryServiceGroupMember.ps1 | 76 ++++++++++++++++ ...h-TssDirectoryServiceGroupMember.Tests.ps1 | 24 +++++ 4 files changed, 201 insertions(+) create mode 100644 docs/commands/directory-services/Search-TssDirectoryServiceGroupMember.md create mode 100644 src/Thycotic.SecretServer/classes/directory-services/GroupMember.cs create mode 100644 src/functions/directory-services/Search-TssDirectoryServiceGroupMember.ps1 create mode 100644 tests/directory-services/Search-TssDirectoryServiceGroupMember.Tests.ps1 diff --git a/docs/commands/directory-services/Search-TssDirectoryServiceGroupMember.md b/docs/commands/directory-services/Search-TssDirectoryServiceGroupMember.md new file mode 100644 index 00000000..97f7b999 --- /dev/null +++ b/docs/commands/directory-services/Search-TssDirectoryServiceGroupMember.md @@ -0,0 +1,89 @@ +# Search-TssDirectoryServiceGroupMember + +## SYNOPSIS +Search Groups in a Directory Service for members + +## SYNTAX + +``` +Search-TssDirectoryServiceGroupMember [-TssSession] -DomainId -GroupName + [] +``` + +## DESCRIPTION +Search Groups in a Directory Service for members + +## EXAMPLES + +### EXAMPLE 1 +``` +$session = New-TssSession -SecretServer https://alpha -Credential $ssCred +Search-TssDirectoryServiceGroupMember -TssSession $session -DomainId 4 -GroupName 'Secret Server Group 4' +``` + +Return members of Group "Secret Server Group 4" from Domain ID 4 + +## PARAMETERS + +### -TssSession +TssSession object created by New-TssSession for authentication + +```yaml +Type: Session +Parameter Sets: (All) +Aliases: + +Required: True +Position: 1 +Default value: None +Accept pipeline input: True (ByValue) +Accept wildcard characters: False +``` + +### -DomainId +Domain ID + +```yaml +Type: Int32 +Parameter Sets: (All) +Aliases: + +Required: True +Position: Named +Default value: 0 +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -GroupName +Group Name + +```yaml +Type: String +Parameter Sets: (All) +Aliases: Name + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Thycotic.PowerShell.DirectoryServices.GroupMember +## NOTES +Requires TssSession object returned by New-TssSession + +## RELATED LINKS + +[https://thycotic-ps.github.io/thycotic.secretserver/commands/directory-services/Search-TssDirectoryServiceGroupMember](https://thycotic-ps.github.io/thycotic.secretserver/commands/directory-services/Search-TssDirectoryServiceGroupMember) + +[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/directory-services/Search-TssDirectoryServiceGroupMember.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/directory-services/Search-TssDirectoryServiceGroupMember.ps1) + diff --git a/src/Thycotic.SecretServer/classes/directory-services/GroupMember.cs b/src/Thycotic.SecretServer/classes/directory-services/GroupMember.cs new file mode 100644 index 00000000..8922fd87 --- /dev/null +++ b/src/Thycotic.SecretServer/classes/directory-services/GroupMember.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; +using System.Management.Automation; +using System.Management.Automation.Runspaces; + +namespace Thycotic.PowerShell.DirectoryServices +{ + public class GroupMember + { + public string DisplayName { get; set; } + } +} \ No newline at end of file diff --git a/src/functions/directory-services/Search-TssDirectoryServiceGroupMember.ps1 b/src/functions/directory-services/Search-TssDirectoryServiceGroupMember.ps1 new file mode 100644 index 00000000..7defd04e --- /dev/null +++ b/src/functions/directory-services/Search-TssDirectoryServiceGroupMember.ps1 @@ -0,0 +1,76 @@ +function Search-TssDirectoryServiceGroupMember { + <# + .SYNOPSIS + Search Groups in a Directory Service for members + + .DESCRIPTION + Search Groups in a Directory Service for members + + .LINK + https://thycotic-ps.github.io/thycotic.secretserver/commands/directory-services/Search-TssDirectoryServiceGroupMember + + .LINK + https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/directory-services/Search-TssDirectoryServiceGroupMember.ps1 + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + Search-TssDirectoryServiceGroupMember -TssSession $session -DomainId 4 -GroupName 'Secret Server Group 4' + + Return members of Group "Secret Server Group 4" from Domain ID 4 + + .NOTES + Requires TssSession object returned by New-TssSession + #> + [CmdletBinding()] + [OutputType('Thycotic.PowerShell.DirectoryServices.GroupMember')] + param ( + # TssSession object created by New-TssSession for authentication + [Parameter(Mandatory,ValueFromPipeline,Position = 0)] + [Thycotic.PowerShell.Authentication.Session] + $TssSession, + + # Domain ID + [Parameter(Mandatory)] + [int] + $DomainId, + + # Group Name + [Parameter(Mandatory)] + [Alias('Name')] + [string] + $GroupName + ) + begin { + $tssParams = $PSBoundParameters + $invokeParams = . $GetInvokeApiParams $TssSession + } + process { + Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)" + if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { + . $CheckVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation + $uri = $TssSession.ApiUrl, 'directory-services', 'domains', $DomainId, 'members' -join '/' + $uri = $uri, "groupName=$GroupName" -join '?' + $invokeParams.Uri = $uri + $invokeParams.Method = 'GET' + + Write-Verbose "Performing the operation $($invokeParams.Method) $($invokeParams.Uri)" + try { + $apiResponse = Invoke-TssApi @invokeParams + $restResponse = . $ProcessResponse $apiResponse + } catch { + Write-Warning "Issue on search request" + $err = $_ + . $ErrorHandling $err + } + + if ($restResponse.records.Count -le 0 -and $restResponse.records.Length -eq 0) { + Write-Warning "No DirectoryServiceGroupMember found" + } + if ($restResponse.members) { + [Thycotic.PowerShell.DirectoryServices.GroupMember[]]$restResponse.members + } + } else { + Write-Warning "No valid session found" + } + } +} \ No newline at end of file diff --git a/tests/directory-services/Search-TssDirectoryServiceGroupMember.Tests.ps1 b/tests/directory-services/Search-TssDirectoryServiceGroupMember.Tests.ps1 new file mode 100644 index 00000000..56edff29 --- /dev/null +++ b/tests/directory-services/Search-TssDirectoryServiceGroupMember.Tests.ps1 @@ -0,0 +1,24 @@ +BeforeDiscovery { + $commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf +} +Describe "$commandName verify parameters" { + BeforeDiscovery { + [object[]]$knownParameters = 'TssSession', 'DomainId', 'GroupName' + [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 Thycotic.PowerShell.DirectoryServices.GroupMember" -TestCases $commandDetails { + $_.OutputType.Name | Should -Be 'Thycotic.PowerShell.DirectoryServices.GroupMember' + } + } +} \ No newline at end of file