Skip to content

Commit

Permalink
Set-SecretTemplate - new command
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Jun 10, 2021
1 parent 73d5217 commit 0dbee8e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/functions/secret-templates/Set-SecretTemplate.ps1
@@ -0,0 +1,86 @@
function Set-SecretTemplate {
<#
.SYNOPSIS
Set Secret Template active or inactive
.DESCRIPTION
Set Secret Template active or inactive
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Set-TssSecretTemplate -TssSession $session -Id 6093 -Active:$false
Sets Secret Template 6093 Active to false or90
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/Set-TssSecretTemplate
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/Folder name/Set-SecretTemplate.ps1
.NOTES
Requires TssSession object returned by New-TssSession
#>
[cmdletbinding(SupportsShouldProcess)]
param(
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,ValueFromPipeline,Position = 0)]
[TssSession]
$TssSession,

# Secret Template ID
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias("TemplateId")]
[int[]]
$Id,

# Set template active
[Parameter(Mandatory)]
[switch]
$Active
)
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.000000' $PSCmdlet.MyInvocation
foreach ($template in $Id) {
$restResponse = $null
$uri = $TssSession.ApiUrl, 'secret-templates', $template -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'PATCH'

$setBody = @{
data = @{
active = @{
dirty = $true
value = [boolean]$Active
}
}
}
$invokeParams.Body = $setBody | ConvertTo-Json

if ($PSCmdlet.ShouldProcess("Secret Template ID: $template", "$($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n")) {
Write-Verbose "Performing the operation $($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n"
try {
$restResponse = . $InvokeApi @invokeParams
} catch {
Write-Warning "Issue setting Secret Template [$template]"
$err = $_
. $ErrorHandling $err
}
if ($restResponse.success) {
Write-Verbose "Secret Template [$template] set to $Active"
} else {
Write-Warning "Secret Template [$template] not updated"
}
}
}
} else {
Write-Warning "No valid session found"
}
}
}
19 changes: 19 additions & 0 deletions tests/secret-templates/Set-SecretTemplate.Tests.ps1
@@ -0,0 +1,19 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Id', 'Active'
[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
}
}
}

0 comments on commit 0dbee8e

Please sign in to comment.