Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
300 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Management.Automation; | ||
using System.Management.Automation.Runspaces; | ||
|
||
namespace Thycotic.PowerShell.Metadata | ||
{ | ||
public class Field | ||
{ | ||
public DateTime? CreateDateTime { get; set; } | ||
public int CreateUserId { get; set; } | ||
public string CreateUserName { get; set; } | ||
public int ItemId { get; set; } | ||
public int MetadataFieldId { get; set; } | ||
public string MetadataFieldName { get; set; } | ||
public int MetadataFieldSectionId { get; set; } | ||
public string MetadataFieldSectionName { get; set; } | ||
public int MetadataFieldTypeId { get; set; } | ||
public string MetadataFieldTypeName { get; set; } | ||
public int MetadataItemDataId { get; set; } | ||
public string MetadataTypeName { get; set; } | ||
public int SortOrder { get; set; } | ||
public bool ValueBit { get; set; } | ||
public DateTime? ValueDateTime { get; set; } | ||
public int ValueInt { get; set; } | ||
public double ValueNumber { get; set; } | ||
public string ValueString { get; set; } | ||
public string ValueUserDisplayName { get; set; } | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
function New-TssMetadataField { | ||
<# | ||
.SYNOPSIS | ||
Create a metadata field for an item | ||
.DESCRIPTION | ||
Create a metadata field for an item | ||
.EXAMPLE | ||
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred | ||
$newMetaParams = @{ | ||
TssSession = $session | ||
ItemId = 5 | ||
ItemType = 'User' | ||
SectionId = 1 | ||
FieldName = 'DeleteMeOn' | ||
FieldDataType = 'DateTime' | ||
FieldValue = '2021-12-31 11:59:59 PM' | ||
} | ||
New-TssMetadataField @newMetaParams | ||
Create the metadata field DeleteMeOn with a DateTime value of December 31, 2021 11:59:59 PM on User ID 5, in current Field Section ID 1 | ||
.EXAMPLE | ||
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred | ||
$newMetaParams = @{ | ||
TssSession = $session | ||
ItemId = 5 | ||
ItemType = 'User' | ||
SectionName = 'TempInfo' | ||
SectionDescription = 'Information that is temporary' | ||
FieldName = 'CurrentOwner' | ||
FieldDataType = 'User' | ||
FieldValue = 6 | ||
} | ||
New-TssMetadataField @newMetaParams | ||
Create the metadata field CurrentOwner with a User ID set to 6, under new section called "TempInfo" (with a description) | ||
.LINK | ||
https://thycotic-ps.github.io/thycotic.secretserver/commands/metadata/New-TssMetadataField | ||
.LINK | ||
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/metadata/New-TssMetadataField.ps1 | ||
.NOTES | ||
Requires TssSession object returned by New-TssSession | ||
#> | ||
[CmdletBinding(SupportsShouldProcess)] | ||
[OutputType('Thycotic.PowerShell.Metadata.Field')] | ||
param ( | ||
# TssSession object created by New-TssSession for authentication | ||
[Parameter(Mandatory,ValueFromPipeline,Position = 0)] | ||
[Thycotic.PowerShell.Authentication.Session] | ||
$TssSession, | ||
|
||
# Item ID | ||
[Parameter(Mandatory, ParameterSetName = 'currentsection')] | ||
[Parameter(Mandatory, ParameterSetName = 'newsection')] | ||
[int] | ||
$ItemId, | ||
|
||
# Metadata Type | ||
[Parameter(Mandatory, ParameterSetName = 'currentsection')] | ||
[Parameter(Mandatory, ParameterSetName = 'newsection')] | ||
[Thycotic.PowerShell.Enums.MetadataType] | ||
$ItemType, | ||
|
||
# Field Section ID | ||
[Parameter(ParameterSetName = 'currentsection')] | ||
[int] | ||
$SectionId, | ||
|
||
# Field Section Name | ||
[Parameter(ParameterSetName = 'newsection')] | ||
[string] | ||
$SectionName, | ||
|
||
# Field Section Description | ||
[Parameter(ParameterSetName = 'newsection')] | ||
[string] | ||
$SectionDescription, | ||
|
||
# Field Name | ||
[Parameter(Mandatory, ParameterSetName = 'currentsection')] | ||
[Parameter(Mandatory, ParameterSetName = 'newsection')] | ||
[string] | ||
$FieldName, | ||
|
||
# Field Data Type | ||
[Parameter(Mandatory, ParameterSetName = 'currentsection')] | ||
[Parameter(Mandatory, ParameterSetName = 'newsection')] | ||
[Thycotic.PowerShell.Enums.MetadataFieldDataType] | ||
$FieldDataType, | ||
|
||
# Item Value | ||
[Parameter(Mandatory, ParameterSetName = 'currentsection')] | ||
[Parameter(Mandatory, ParameterSetName = 'newsection')] | ||
[string] | ||
$FieldValue, | ||
|
||
# Requires Administer Metadata permission to modify | ||
[Parameter(ParameterSetName = 'currentsection')] | ||
[Parameter(ParameterSetName = 'newsection')] | ||
[switch] | ||
$RequireAdminister, | ||
|
||
# Requires Edit permission on the Item to modify | ||
[Parameter(ParameterSetName = 'currentsection')] | ||
[Parameter(ParameterSetName = 'newsection')] | ||
[switch] | ||
$RequireItemEdit | ||
) | ||
begin { | ||
$tssNewParams = $PSBoundParameters | ||
$invokeParams = . $GetInvokeApiParams $TssSession | ||
} | ||
process { | ||
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)" | ||
if ($tssNewParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { | ||
. $CheckVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation | ||
$uri = $TssSession.ApiUrl, 'metadata', $ItemType, $ItemId -join '/' | ||
$invokeParams.Uri = $uri | ||
$invokeParams.Method = 'POST' | ||
|
||
$newBody = @{data = @{} } | ||
switch ($tssNewParams.Keys) { | ||
'FieldDataType' { $newBody.data.Add('fieldDataType',[string]$FieldDataType) } | ||
'FieldValue' { | ||
switch ($FieldDataType) { | ||
'String' { $newBody.data.Add('valueString', $FieldValue) } | ||
'Boolean' { $newBody.data.Add('valueBit',[boolean]$FieldValue) } | ||
'Number' { $newBody.data.Add('valueNumber',$FieldValue) } | ||
'DateTime' { $newBody.data.Add('valueDateTime',[datetime]$FieldValue) } | ||
'User' { $newBody.data.Add('valueInt',[int]$FieldValue) } | ||
} | ||
} | ||
'FieldName' { $newBody.data.Add('metadataFieldName',$FieldName) } | ||
'SectionId' { $newBody.data.Add('metadataFieldSectionId',$SectionId) } | ||
'SectionName' { $newBody.data.Add('metadataFieldSectionName',$SectionName) } | ||
'SectionDescription' { $newBody.data.Add('metadataFieldSectionDescription',$SectionDescription) } | ||
'RequireAdminister' { $newBody.data.Add('metadataFieldSectionRequiresAdministerMetadata',[boolean]$RequireAdminister) } | ||
'RequireItemEdit' { $newBody.data.Add('metadataFieldSectionRequiresEntityEdit',[boolean]$RequireItemEdit) } | ||
} | ||
|
||
$invokeParams.Body = ($newBody | ConvertTo-Json -Depth 10) | ||
|
||
Write-Verbose "Performing the operation $($invokeParams.Method) $uri with: `n$($invokeParams.Body)`n" | ||
if (-not $PSCmdlet.ShouldProcess("Item ID: $ItemId", "$($invokeParams.Method) $uri with: `n$($invokeParams.Body)`n")) { return } | ||
try { | ||
$apiResponse = Invoke-TssApi @invokeParams | ||
$restResponse = . $ProcessResponse $apiResponse | ||
} catch { | ||
Write-Warning "Issue creating Metadata Field on Item [$ItemId]" | ||
$err = $_ | ||
. $ErrorHandling $err | ||
} | ||
|
||
if ($restResponse) { | ||
[Thycotic.PowerShell.Metadata.Field]$restResponse | ||
} | ||
} else { | ||
Write-Warning "No valid session found" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
BeforeDiscovery { | ||
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf | ||
} | ||
Describe "$commandName verify parameters" { | ||
BeforeDiscovery { | ||
[object[]]$knownParameters = 'TssSession', 'ItemId', 'ItemType', 'SectionId', 'SectionName', 'SectionDescription', 'FieldName', 'FieldDataType', 'FieldValue', 'RequireAdminister', 'RequireItemEdit' | ||
[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.Metadata.Field" -TestCases $commandDetails { | ||
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.Metadata.Field' | ||
} | ||
} | ||
} |