Adding Windows Credential to Windows Credential Manager Using Log on script - Using Azure AD joined VM

Jan Nuaman 0 Reputation points
2024-02-26T21:18:48.8833333+00:00

Hello there, I have 20 VMs joined to Azure AD, using the below command in script.bat:

cmdkey /add:ServerName1 /user:DomainName\%USERNAME% /pass:%password%> cmdkey /add:ServerName2 /user:DomainName\%USERNAME% /pass:%password%

Add the credentials to Windows Credential Manager successfully. However: when I am running the script using local group policy: User configuration > Windows Settings > Scripts(logon/logoff) > logon the rsop.msc shows that the script's last run time is during the logon process! BUT, no Creds get added to the Windows Credential Manager. I decided to add some delay, and renamed the script to be script.ps1 with this context:

Start-Sleep -Seconds 30> cmdkey /add:ServerName1 /user:DomainName\%USERNAME% /pass:%password%> cmdkey /add:ServerName2 /user:DomainName\%USERNAME% /pass:%password%

I also tried the options shown below, but none of them worked: PowerShell

Any idea on how to make this simple task work?

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,393 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 35,996 Reputation points Microsoft Vendor
    2024-02-28T04:53:51.8366667+00:00

    Hi Jan Nuaman,

    Please note that %USERNAME% and %password% are not available in PowerShell. Please use $env:USERNAME and $env:password to refer to the environment variables.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.