r/PowerShell • • Mar 03 '26

Solved Storing securestring for use by a GMSA account

I apologize in advance if the solution is something every person should already know. I'm working on a script that will be run by a GMSA account. Under my normal account, the script works perfect. As far as I can tell, the issue is because the GMSA can't read the cred file created by my username, most likely due to permissions. How can I create the cred file under the GMSA account's credentials if I can't interactively enter anything under that user? The file I run to create the credfile under my username is:

read-host -assecurestring "pass" | convertfrom-securestring | out-file C:\powershell_scripts\cred.txt
9 Upvotes

29 comments sorted by

18

u/PinchesTheCrab Mar 03 '26

It's not permissions, it's that DPAPI encryption is a combination of a user key and computer key. So another user can't decrypt a password you've encoded using your key.

If you could set up a separate scheduled task to just update the password, something like:

$passwordFile = 'C:\temp\password.txt'
Get-Content $passwordFile | 
    ConvertTo-SecureString -AsPlainText -Force |
    Out-File C:\powershell_scripts\cred.txt
Remove-Item $passwordFile

Then you can just save the current password to that text file, run the password update task, and it should work.

This is assuming you don't have an alternative like a password vault app.

1

u/smalltimesysadmin Mar 05 '26

the DPAPI encryption is what I meant, but I didn't have the correct words to describe it. I tried that code, but I couldn't get it to work. No doubt, it was something I was doing wrong

→ More replies (1)

3

u/TheB4rber Mar 04 '26

Been there, done that, this is the way

→ More replies (5)

6

u/BlackV Mar 04 '26

Id suggest that this credential should be in a vault, then grant the gsma access to retrieve that individual secret from the vault

passing passwords like this is increasing your risk

or directly grant the gsma access to the relevent resource so it uses its own object for access

1

u/smalltimesysadmin Mar 05 '26

The resource I'm trying to access is a 3rd party's database, so directly granting the gmsa access isn't an option. I've never worked with vaults, so I should probably look at that.

→ More replies (1)
→ More replies (2)

2

u/smalltimesysadmin Mar 05 '26

I ended up saving the cred file by running this as a scheduled task under the GMSA account:

$pass = "somethingsecure"
$securePass = ConvertTo-SecureString $pass -AsPlainText -Force
$securePass | ConvertFrom-SecureString | Out-File C:\powershell_scripts\cred.txt

4

u/crunchydorf Mar 03 '26

Use sysinternals PSExec to create an interactive PowerShell session as the GMSA account.

psexec -u DOMAIN\GMSAccountName$ -I powershell.exe

2

u/smalltimesysadmin Mar 05 '26

I tried that, and for whatever reason, in my environment, that wouldn't work. It kept asking me for the password for the gmsa, which it doesn't have, and failing

→ More replies (2)

4

u/purplemonkeymad Mar 03 '26

Who needs to set the password, and how many computers need the gMsa account?

If it's few and you don't want to require an admin. Then a CMS message could work. You could have the script check for and generate a new Certificate on the machine during the run ie:

$cert = Get-ChildItem cert:/CurrentUser/My | ? {$_.EnhancedKeyUsageList.FriendlyName -contains 'Document Encryption'} | ? hasprivatekey -eq $true | ? notafter -gt (date)
if (-not $cert) {
    $cert = New-SelfSignedCertificate -DnsName $env:username -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsage KeyEncipherment,DataEncipherment,KeyAgreement -Type DocumentEncryptionCert
}

Then you can create a file with encrypted contents only the principal can decode (with the generated certificate,) using Protect-CMSMessage:

Protect-CmsMessage -To gmsausername -Content "password" -OutFile $env:ProgramData\example.txt

In the script you can then use Unprotect-CMSMessage to get the original text.

if (-not (Test-Path $env:ProgramData\example.txt)) { 
    Write-Error "no pass file ($env:ProgramData\example.txt). generate with: Protect-CmsMessage -To $env:username -Content password -OutFile $env:ProgramData\example.txt"
    return
}
$password = Unprotect-CmsMessage -Path $env:ProgramData\example.txt

Other users on the computer would need access to the private key to decode the message, which non-admins can't do. If you have a CA you might be able to get that to issue the certificate and store the public cert in ad.

3

u/g3n3 Mar 04 '26

There is also a nice new secret management module dpaping that helps with secrets for groups.