Retrieving Secrets by name when the name is a variable

Retrieving Secrets by name when the name is a variable

avatar
(anonymous user)
Product: PowerShell Universal
Version: 2026.1.3

Hi everyone,

First of all, let me disclose tht I’m far from being an expert on PSU since I have only started working with it in a test environment recently, neither am I a professional programmer - I’m a hobbyist at best.

I’ve been prototyping a PSU-based solution to replace an old internal system and I’ve been experimenting with PSU Variables and Secrets.

To sum-up what I’m trying to do:

Our users need to log into to various Web services with generic accounts (ex: one account per geographical site on each service) but naturally, we don’t want to share the credentials openly with everyone who needs access.

For this, they have an SSO-based intranet dashboard that calls web automation scripts (which I’m also working on converting from an older technology to Playwright).

I managed, by script, to export and import Secrets into PSU in the PSCredential format and when I try to retrieve them individually, everything works no problem.

However, the way I wanted to do things was to have one script per service, which would then fetch the right set of credentials based on the user’s geographical site and potnetially other variables.

That’s where my issue arises:

Our Secret variables are named following a structure like say ServiceName_GeoSiteName

So if I construct the variable by doing

$SecretName = $ServiceName+“_”+$UserGeoSite

I cannot seem to find a way to use $SecretName to call the Secret with

$Secret:$SecretName

Unexpected token ‘$SecretName’ in expression or statement.

I’ve tried fiddling around whith brackets, parenthesis, moving the $ sign about but I cannot figure a way to dynamically refer to a Secret.

Is there a way to call a Secret like this where the name of the Secret is not known prior to user interaction?

Am I going at it wrong / is my logic flawed?

Thank you for your help!

All Comments (15)

avatar
(anonymous user)

You could do it with subexpressions and the secret provider.

Here is an older post that may help:

Product: PowerShell Universal Version: 4.3.0 Hi, Quick question - we have several secrets with “-” in the name, and when we attempt to retrieve them using the normal way ($Secret:Secret-Name), then it dosent work. How can i retrieve the secrets, if the name of the secrets contains a “-” ?

avatar
(anonymous user)

Thank you for your reply, do you have any specific pointers as to how to do it?

I cannot seem to get it to work, I’ve tried the forms mentioned in the linked thread (and the link within as well) and tinkering with them but to no avail.

For example if I attempt to use composed structures such as:

$("Secret:$SecretName")

It simply ends up printing

Secret:$SecretName

Whereas various attempts at

$(Get-ChildItem -Path "Secret:$SecretName")

Throws the following error:

Cannot find path 'NameOfMySecret' because it does not exist.

Is that what you meant by subexpressions?

avatar
(anonymous user)

Try it like this:

$SecretName = “name”

Get-ChildItem -Path “Secret:$($SecretName)”

edit:

After checking again the subexpression is not needed.

This works for me:

Get-ChildItem -Path "Secret:$SecretName"

avatar
(anonymous user)

Okay, so your solution works and I realize that I’m being hindered by an underlying issue I had not detected sooner.

For reference, here is my testing code:

$tool = 'WebService'
$site = 'SiteID'
$SecretName = $tool+'_'+$site

$user = 'UserName01'
$passw = 'PassWord01!'
$passw = ConvertTo-SecureString $passw -AsPlainText -Force #Please don't shame me for insecure practices
$credsObject = New-Object System.Management.Automation.PSCredential($user,$passw)

#Creating the secret
New-PSUVariable -Name $SecretName -Vault "Database" -Type "PSCredential" -InputObject $credsObject -Description "Test variable for secret creation via script" -Tag "Playwright" -TrustCertificate

#Retrieving it as a test
Get-ChildItem -Path "Secret:$($SecretName)"

When I run it, the Secret is seemingly correctly created

(Can’t post screenshot of the successful prompt window creating the secret because media limit)

But the retrieval fails with error:

Cannot find path 'WebService_SiteID' because it does not exist.
at <ScriptBlock>, [Redacted]\Test_CreateSecrets.ps1: line 14

However, if I create the secret with the same information but manually using the PSU Variables GUI (and remove the New-PSUVariable line) it works perfectly

(Can’t post screenshot of the successful prompt window returning the username and password because media limit)

Clearly, the problem appears to be with Secrets created automatically through scripts… If you have any idea of what I’m doing wrong with that New-PSUVariable command, I’d be a big help!

Thank you again

avatar
(anonymous user)

(Edit: fake news, i take it back)

Oh yeah, you can only create variables with New-PSUVariable, not secrets. So your script will probably work if you leave out the “Secret:” part.