Skip to content

Commit

Permalink
Search-TssUserGroup - new command
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Jan 21, 2021
1 parent 26423f2 commit ba60175
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Thycotic.SecretServer.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,58 @@
</TableControl>
</View>

<!-- Getting TssGroupSummary -->
<View>
<Name>TssGroupSummary</Name>
<ViewSelectedBy>
<TypeName>TssGroupSummary</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>GroupId</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>GroupName</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Created</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Enabled</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>MemberCount</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>DomainName</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Synchronized</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>

</ViewDefinitions>
</Configuration>
13 changes: 13 additions & 0 deletions src/Thycotic.SecretServer.Types.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@
</AliasProperty>
</Members>
</Type>
<Type>
<Name>TssGroupSummary</Name>
<Members>
<AliasProperty>
<Name>GroupId</Name>
<ReferencedMemberName>Id</ReferencedMemberName>
</AliasProperty>
<AliasProperty>
<Name>GroupName</Name>
<ReferencedMemberName>Name</ReferencedMemberName>
</AliasProperty>
</Members>
</Type>
</Types>
22 changes: 22 additions & 0 deletions src/classes/usermanagement/TssGroupSummary.class.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class TssGroupSummary {
# Created date
[datetime]$Created
# Domain Directory Group GUID
[string]$DomainGuid
# Directory Domain ID
[int]$DomainId
# Directory Domain Name
[string]$DomainName
# Group is Active
[boolean]$Enabled
# Group ID
[int]$Id
# Number of members
[int]$MemberCount
# Group Name
[string]$Name
# Group is synchronized with Active Directory
[boolean]$Synchronized
# Active Directory Sync will pull members for domain groups
[boolean]$SynchronizeNow
}
103 changes: 103 additions & 0 deletions src/functions/usermanagement/Search-UserGroup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
function Search-UserGroup {
<#
.SYNOPSIS
Search for user management groups
.DESCRIPTION
Search for user management groups
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Search-UserGroup -TssSession $session
Return list of all groups found in Secret Server that account has access to manage
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType('TssGroupSummary')]
param (
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0)]
[TssSession]$TssSession,

# Active Directory Domain Id
[int]
$DomainId,

# Include inactive groups in results
[switch]
$IncludeInactive,

# Text to search for group name
[string]
$SearchText,

# Sort by specific property, default Name
[string]
$SortBy = 'Name',

# Output the raw response from the API endpoint
[switch]
$Raw
)
begin {
$tssParams = . $SearchUserGroupParams $PSBoundParameters
$invokeParams = @{ }

$groupParams = . $SearchUserGroupParams $PSBoundParameters
$groupParams.Remove('TssSession')
$groupParams.Remove('Raw')
}

process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
$uri = $TssSession.ApiUrl, 'groups' -join '/'
$uri += "?sortBy[0].direction=asc&sortBy[0].name=$SortBy&take=$($TssSession.Take)"

$filters = @()
if ($groupParams.Contains('DomainId')) {
$filters += "filter.domainId=$DomainId"
}
if ($groupParams.Contains('IncludeInactive')) {
$filters += "filter.includeInactive=$IncludeInactive"
}
if ($groupParams.Contains('SearchText')) {
$filters += "filter.searchText=$SearchText"
}
if ($filters) {
$uriFilter = $filters -join '&'
Write-Verbose "Filters: $uriFilter"
$uri = $uri, $uriFilter -join '&'
}

$invokeParams.Uri = $uri
$invokeParams.PersonalAccessToken = $TssSession.AccessToken
$invokeParams.Method = 'GET'
Write-Verbose "$($invokeParams.Method) $uri"
try {
$restReponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue on search request"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
return $restReponse
}
if ($restReponse.records.Count -le 0 -and $restReponse.records.Length -eq 0) {
Write-Warning "No groups found"
}
if ($restReponse.records) {
. $TssGroupSummaryObject $restReponse.records
}
} else {
Write-Warning "No valid session found"
}
}
}
35 changes: 35 additions & 0 deletions src/parts/SearchUserGroupParams.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<#
.Synopsis
Gets Search-TssUserGroup's parameters
.Description
Gets the parameters for Search-TssUserGroup from a collection of parameters
#>
param(
# A collection of parameters. Parameters not used in Search-TssUserGroup will be removed
[Parameter(ValueFromPipeline,Position = 0,Mandatory,ParameterSetName = 'GetParameterValues')]
[Alias('Parameters')]
[Collections.IDictionary]
$Parameter
)

begin {
if (-not ${script:Search-TssUserGroup}) {
# If we haven't cached a reference to Search-TssUserGroup,
${script:Search-TssUserGroup} = # make it so.
[Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand('Search-TssUserGroup', 'Function')
}
}

process {
if ($PSCmdlet.ParameterSetName -eq 'GetParameterValues') {
$searchReportSched = [Ordered]@{ } + $Parameter # Then we copy our parameters
foreach ($k in @($searchReportSched.Keys)) {
# and walk thru each parameter name.
# If a parameter isn't found in Search-TssUserGroup
if (-not ${script:Search-TssUserGroup}.Parameters.ContainsKey($k)) {
$searchReportSched.Remove($k) # we remove it.
}
}
return $searchReportSched
}
}
29 changes: 29 additions & 0 deletions src/parts/TssGroupSummaryObject.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<#
.Synopsis
Creates a TssGroupSummary class in the Thycotic.SecretServer module.
.Description
Creates an instance of the TssGroupSummary class to output the GroupSummary object
#>
param(
[pscustomobject]$Object
)

begin {
$Properties = $Object[0].PSObject.Properties.Name
}

process {
$outObject = @()
foreach ($g in $Object) {
$outGroupSummary = [TssGroupSummary]::new()
foreach ($rsProp in $Properties) {
if ($rsProp -in $outGroupSummary.PSObject.Properties.Name) {
$outGroupSummary.$rsProp = $g.$rsProp
} else {
Write-Warning "Property $rsProp does not exist in the TssGroupSummary class. Please create a bug report at https://github.com/thycotic-ps/thycotic.secretserver/issues/new/choose"
}
}
$outObject += $outGroupSummary
}
return $outObject
}
25 changes: 25 additions & 0 deletions tests/usermanagement/Search-TssUserGroup.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', 'Raw', 'DomainId', 'IncludeInactive', 'SearchText', 'SortBy'
[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 parmaeters" -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 TssGroupSummary" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'TssGroupSummary'
}
}
}

0 comments on commit ba60175

Please sign in to comment.