cmdkey in PowerShell doesn’t work when run as a logon script?

Question:

Trying is use cmdkey in a PowerShell logon script to store credentials in the credential manager. When the script is run from PowerShell ISE everything works, but when it’s run as a logon script via Group Policy everything but cmdkey works. Cannot for the life of me figure out why cmdkey will work everywhere except when the script run on logon.

Answer:

I imagine cmdkey is using Microsoft’s Data Protection API (DPAPI) to encrypt credentials so only the current user can retrieve them. You can’t use this API unless the user’s session is loaded. When your script runs, it may be too early in the logon process for the security information the DPAPI needs is loaded. I’m not sure how logon scripts work, but try putting a delay in your logon script until you get a value back.

Here’s the PowerShell code that encrypts with the DPAPI:

Add a loop in your logn script that tries to encrypt/decrypt some random array of bytes until it succeeds.

Source:

cmdkey in PowerShell doesn’t work when run as a logon script? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply