Skip to content

Commit

Permalink
Search-Script command
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Jun 3, 2021
1 parent 10e7862 commit efc8d24
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/classes/scripts/TssScriptSummary.class.ps1
@@ -0,0 +1,26 @@
class TssScriptSummary {
[boolean]
$Active

[string]
$ConcurrencyId

[string]
$Description

[string]
$Name

[int]
$ScriptCategoryId

[string]
$ScriptCategoryName

[int]
$ScriptId

[ValidateSet('PowerShell','SQL','SSH')]
[string]
$ScriptType
}
44 changes: 44 additions & 0 deletions src/en-us/about_tssscriptsummary.help.txt
@@ -0,0 +1,44 @@
TOPIC
This help topic describes the TssScriptSummary class in the Thycotic.SecretServer module

CLASS
TssScriptSummary

INHERITANCE
None

DESCRIPTION
The TssScriptSummary class represents the ScriptSummary object returned by Secret Server endpoint GET /userscripts

CONSTRUCTORS
new()

PROPERTIES
Active: boolean
Whether the Script is Active

ConcurrencyId: string
Unique Script Concurrency Id

Description: string
Script Description

Name: string
Script Name

ScriptCategoryId: integer (int32)
Script Category Id

ScriptCategoryName: string
Script Category Name

ScriptId: integer (int32)
Script Id

ScriptType: UserScriptType
Script Type (Powershell = 1, SQL = 2, SSH = 3)

METHODS

RELATED LINKS:
Search-TssScript
87 changes: 87 additions & 0 deletions src/functions/scripts/Search-Script.ps1
@@ -0,0 +1,87 @@
function Search-Script {
<#
.SYNOPSIS
Search Secret Server scripts
.DESCRIPTION
Search Secret Server scripts (Admin > Scripts)
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/Search-TssScript
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/scripts/Search-Script.ps1
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Search-TssScript -TssSession $session -SearchText admin
Return list of Scripts that have the text "admin" in the name
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType('TssScriptSummary')]
param (
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,ValueFromPipeline,Position = 0)]
[TssSession]
$TssSession,

# Search Text
[string]
$SearchText,

# Include inactive scripts
[switch]
$IncludeInactive,

# Sort by specific property, default ScriptId
[string]
$SortBy = 'ScriptId'
)
begin {
$tssParams = $PSBoundParameters
$invokeParams = . $GetInvokeTssParams $TssSession
}
process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation
$uri = $TssSession.ApiUrl, 'userscripts' -join '/'
$uri = $uri, "sortBy[0].direction=asc&sortBy[0].name=$SortBy&take=$($TssSession.Take)" -join '?'
$invokeParams.Method = 'GET'

$filters = @()
switch ($tssParams.Keys) {
'SearchText' { $filters += "filter.searchText=$SearchText" }
'IncludeInactive' { $filters += "filter.includeInactive=$([boolean]$IncludeInactive)" }
}
if ($filters) {
$uriFilter = $filters -join '&'
Write-Verbose "Filters: $uriFilter"
$uri = $uri, $uriFilter -join '&'
}
$invokeParams.Uri = $uri

Write-Verbose "Performing the operation $($invokeParams.Method) $uri"
try {
$restResponse = . $InvokeApi @invokeParams
} 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 Script found"
}
if ($restResponse.records) {
[TssScriptSummary[]]$restResponse.records
}
} else {
Write-Warning "No valid session found"
}
}
}
24 changes: 24 additions & 0 deletions tests/scripts/Search-Script.Tests.ps1
@@ -0,0 +1,24 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'SearchText', 'IncludeInactive', '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 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 TssScriptSummary" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'TssScriptSummary'
}
}
}

0 comments on commit efc8d24

Please sign in to comment.