powershell to store user credential for commands

Eaven HUANG 2,191 Reputation points
2022-04-26T02:38:26.51+00:00

Dear Experts,

I'm trying to rename a list of AD computers, however as checked from MS site, following script I just created didn't give me an option to store password, so each line being used, it asked me to manually enter my AD password. What can we enhance at this point so I can store my AD password somewhere and the script will use it? if possible, I don't want to store my password in the script in plain text.

Many thanks!

$List = Import-Csv "C:\Computer_Rename_List.csv"
foreach ($List in $Lists)
{
Rename-Computer `
-ComputerName $List.Current_Name `
-NewName $List.New_Name `
-DomainCredential DOMAIN\ADMIN `
-Force
}
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. T. Kujala 8,761 Reputation points
    2022-04-26T04:06:11.03+00:00
    0 comments No comments

  2. Limitless Technology 39,916 Reputation points
    2022-04-27T08:53:07.15+00:00

    Hello

    Thank you for your question and reaching out.

    I can understand you wish to pass Credentials in PowerShell session.

    The PSCredential object represents a set of security credentials such as a user name and password. The object can be passed as a parameter to a function that runs as the user account in that credential object. There are a few ways that you can create a credential object. The first way to create a credential object is to use the PowerShell cmdlet Get-Credential. When you run without parameters, it prompts you for a username and password. Or you can call the cmdlet with some optional parameters.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-7.2

    ------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  3. Newbie Jones 1,386 Reputation points
    2022-04-27T09:47:18.707+00:00

    I don't think either of these docs on their own meets the original request.
    Which is to avoid manually entering the password and to NOT store passwords in the script.
    Therefore I'm assuming using read-host doesn't meet the requirement.

    I can recommend two options for this.

    Using a 256-bit AES key file and a password file.
    Ensuring you store both files on a secure NTFS share that the account running the script has access to.
    You need to lock down access to the share, as having access to the key file will let you read the password file. Don't store the file in the same location as the script.

    Then it would be something like this.

    $Account = "MyDomain\MyAccount"  
    $Key = Get-Content "\\MyServer\MyShare\AES_KEY_FILE.key"  
    $Credentials = New-Object System.Management.Automation.PSCredential($Account,(Get-Content "\\MyServer\MyShare\AES_PASSWORD_FILE.txt" | ConvertTo-SecureString -Key $Key))  
    

    Read here for more information on how to create the key and password file.
    https://dennisspan.com/encrypting-passwords-in-a-powershell-script

    Second (and preferred) option if you are lucky enough to have access to this in Azure is to use something like KeyVault.

    https://learn.microsoft.com/en-us/azure/key-vault/secrets/quick-create-powershell

    Similar to option 1. You lock down the key vault to the Service Account that will run the script.

    0 comments No comments

  4. Newbie Jones 1,386 Reputation points
    2022-04-27T09:56:48.767+00:00

    On a side note, please don't use domain admins for this.

    Create a service account with least privilege to rename computers.

    If I remember rightly, by default Account Operators and Domain admins have the Active Directory rights to do this on computer objects.

    Account Operators still give far too many permissions for the task at hand though.

    So you can delegate just permissions to rename computer accounts.

    Two permissions are required. The domain account needs "Rename a Computer Account" permissions for the and you have to have an account that is a member of the local administrators account on the computer being renamed. Create a group with these rights and then create a service account specifically for renaming accounts and add it to this group.

    0 comments No comments

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.