Skip to content

Commit

Permalink
Get-TssFolder - Add FolderPath parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Apr 11, 2021
1 parent 233744d commit 05dd198
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/functions/folders/Get-Folder.ps1
Expand Up @@ -24,6 +24,12 @@ function Get-Folder {
Returns folder associated with Folder ID, 93 and include Secret Templates associated with the folder
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Get-TssFolder -TssSession $session -FolderPath '\ABC Company\Security'
Returns folder that has a path of ABC Company\Security
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/Get-TssFolder
Expand All @@ -42,22 +48,29 @@ function Get-Folder {
$TssSession,

# Folder ID to retrieve
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'id')]
[Alias("FolderId")]
[int[]]
$Id,

# Retrieve all child folders within the requested folder
[Parameter(ParameterSetName = 'filter')]
[Parameter(ParameterSetName = 'id')]
[Alias("GetAllChildren")]
[switch]
$GetChildren,

# Include allowable Secret Templates of the requested folder
[Parameter(ParameterSetName = 'filter')]
[Parameter(ParameterSetName = 'id')]
[Alias('IncludeAssociatedTemplates','IncludeTemplates')]
[switch]
$IncludeTemplate
$IncludeTemplate,

# Get folder based on path (e.g. \Parent\child1\child2)
[Parameter(ParameterSetName = 'path')]
[string]
$FolderPath
)
begin {
$tssParams = $PSBoundParameters
Expand All @@ -67,25 +80,33 @@ function Get-Folder {
process {
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation
foreach ($folder in $Id) {
$restResponse = $null
$uri = $TssSession.ApiUrl, 'folders', $folder -join '/'
$uri = $uri + '?' + "getAllChildren=$GetChildren" + "&" + "includeAssociatedTemplates=$IncludeTemplate"

$invokeParams.Uri = $Uri
$invokeParams.Method = 'GET'

Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = . $InvokeApi @invokeParams
} catch {
Write-Warning "Issue getting folder [$folder]"
$err = $_
. $ErrorHandling $err
}

if ($restResponse) {
[TssFolder[]]$restResponse
if ($tssParams.ContainsKey('FolderPath')) {
$pathLeaf = Split-Path $FolderPath -Leaf
$folderFound = . $SearchFolders $TssSession $pathLeaf | Where-Object FolderPath -EQ $FolderPath
$Id = $folderFound.Id
}
if ($tssParams.ContainsKey('Id') -or $Id) {
foreach ($folder in $Id) {
$restResponse = $null
$uri = $TssSession.ApiUrl, 'folders', $folder -join '/'
$uri = $uri + '?' + "getAllChildren=$GetChildren" + "&" + "includeAssociatedTemplates=$IncludeTemplate"

$invokeParams.Uri = $Uri
$invokeParams.Method = 'GET'

Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = . $InvokeApi @invokeParams
} catch {
Write-Warning "Issue getting folder [$folder]"
$err = $_
. $ErrorHandling $err
}

if ($restResponse) {
[TssFolder[]]$restResponse
}
}
}
} else {
Expand Down
41 changes: 41 additions & 0 deletions src/parts/SearchFolders.ps1
@@ -0,0 +1,41 @@
<#
.SYNOPSIS
Search secret folders
#>
[CmdletBinding()]
param (
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,Position = 0)]
[TssSession]
$TssSession,

# Search by text value
[Parameter(Mandatory,Position = 1)]
[string]
$SearchText
)
begin {
$invokeParams = . $GetInvokeTssParams $TssSession
}

process {
$restResponse = $null
$uri = $TssSession.ApiUrl, 'folders' -join '/'
$uri = $uri, "sortBy[0].direction=asc&sortBy[0].name=FolderPath&take=$($TssSession.Take)&filter.folderTypeId=1" -join '?'

$invokeParams.Uri = $uri
$invokeParams.Method = 'GET'

Write-Verbose "$($invokeParams.Method) $uri with $body"
try {
$restResponse = . $InvokeApi @invokeParams
} catch {
Write-Warning "Issue on search request"
$err = $_
. $ErrorHandling $err
}

if ($restResponse.records) {
[TssFolderSummary[]]$restResponse.records
}
}

0 comments on commit 05dd198

Please sign in to comment.