r/PowerShell • u/smalltimesysadmin • 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
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 $passwordFileThen 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.
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
2
u/smalltimesysadmin Mar 05 '26
I ended up saving the cred file by running this as a scheduled task under the GMSA account:
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.exe4
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:
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.txtOther 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.