functions/secret-dependencies/New-SecretDependencyGroup.ps1

function New-SecretDependencyGroup {
    <#
    .SYNOPSIS
    Create a new Dependency Group on a Secret
 
    .DESCRIPTION
    Create a new Dependency Group on a Secret
 
    .EXAMPLE
    $session = New-TssSession -SecretServer https://alpha/SecretServer -Credential (Get-Secret apiacct)
    New-TssSecretDependencyGroup -TssSession $session -Id 330, 342 -GroupName 'QA Env'
 
    Create the Dependency Group "QA Env" on Secrets 330 and 342
 
    .EXAMPLE
    $session = New-TssSession -SecretServer https://alpha/SecretServer -Credential (Get-Secret apiacct)
    Search-TssSecret -TssSession $session -FolderId 50 | New-TssSecretDependencyGroup -TssSession $session -GroupName 'Test Env'
 
    Create the Dependency Group "Test Env" on all Secrets found under Folder ID 50, that the apiacct has access
 
    .LINK
    https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-dependencies/New-TssSecretDependencyGroup
 
    .LINK
    https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-dependencies/New-SecretDependencyGroup.ps1
 
    .NOTES
    Requires TssSession object returned by New-TssSession
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('TssSecretDependencyGroup')]
    param (
        # TssSession object created by New-TssSession for auth
        [Parameter(Mandatory, ValueFromPipeline, Position = 0)]
        [TssSession]
        $TssSession,

        # Secret ID
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('SecretId')]
        [int[]]
        $Id,

        # Name of Group
        [Parameter(Mandatory)]
        [string]
        $GroupName,

        # Site ID, not provided will configure "Site from Secret"
        [int]
        $SiteId
    )
    begin {
        $tssNewParams = $PSBoundParameters
        $invokeParams = . $GetInvokeTssParams $TssSession
    }
    process {
        Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
        if ($tssNewParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
            . $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation
            foreach ($secret in $Id) {
                $restResponse = $null
                $uri = $TssSession.ApiUrl, 'secret-dependencies', 'groups', $secret -join '/'
                $invokeParams.Uri = $uri
                $invokeParams.Method = 'POST'

                $newBody = [ordered]@{}
                switch ($tssNewParams.Keys) {
                    'GroupName' { $newBody.Add('secretDependencyGroupName', $GroupName) }
                    'SiteId' { $newBody.Add('siteId', $SiteId) }
                }
                $invokeParams.Body = ($newBody | ConvertTo-Json)

                Write-Verbose "Performing the operation $($invokeParams.Method) $uri with:`n $newBody"
                if (-not $PSCmdlet.ShouldProcess('', "$($invokeParams.Method) $uri with $($invokeParams.Body)")) { return }
                try {
                    $restResponse = . $InvokeApi @invokeParams
                } catch {
                    Write-Warning "Issue creating Dependency Group [$Name] on Secret [$secret]"
                    $err = $_
                    . $ErrorHandling $err
                }

                if ($restResponse) {
                    [TssSecretDependencyGroup]$restResponse
                }
            }
        } else {
            Write-Warning 'No valid session found'
        }
    }
}