KNOWLEDGE PORTAL

Pulling a Secret from Secret Server by ID

Summary

**Do not use in production environment without professional assistance**

One of the most common things clients look to do when scripting is to pull a secret from Secret Server. Below is the code that can be used in PowerShell to pull a secret by ID. 

To view this script on the Cyberhill Github page, click here.


Function Get-SSSecretByID {
    Param
    (
         [Parameter(Mandatory=$true, Position=0)]
         [string] $URI,
         [Parameter(Mandatory=$true, Position=1)]
         [string] $APIKey,
         [Parameter(Mandatory=$true, Position=2)]
         [int] $SecretID,
         [Parameter(Mandatory=$false, Position=3)]
         [string] $IncludeInactive = $false
    )

    try
    {
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", "Bearer $APIKey")

    $URL = "$URI/api/v1/secrets/$SecretID" + "?IncludeInactive=$IncludeInactive"

    $SecretDetails = Invoke-RestMethod $URL -Headers $headers
    return $SecretDetails
    }
    catch
    {
        $result = $_.Exception.Response.GetResponseStream();
        $reader = New-Object System.IO.StreamReader($result);
        $reader.BaseStream.Position = 0;
        $reader.DiscardBufferedData();
        $responseBody = $reader.ReadToEnd() | ConvertFrom-Json
        Write-Host "ERROR: ($responseBody.error)"
        return;
    }
} 
You might also be interested in
Github Repository
Cyberhill creates hundreds of custom API scripts for clients. Visit our Github page for a full list.
LEARN MORE
Folder Information
When creating a folder, it is just as important to know what the API is looking for as it is to be able to make the API call to create…
LEARN MORE
Pulling a User Account
When working with Secret Server it is important to know what groups and roles a user is part of.
LEARN MORE