Powershell/Vb Script to rename PC and add to a domain using encrypted domain credentials

Hello,
I need a script to rename a computer and join it to the domain. I need the domain credentials to be encrypted since this task is being out sourced.

Thank you
LVL 1
SeanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

A. Cristian CsikiSenior System AdministratorCommented:
Hi,

You need to request user's input or you need to have already stored data inside?

If you need to encrypt the data and send it like this you can use this case to encrypt it:

$password = "password" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString

Once encrypt it you use the new variable like this:

$password = "01000000d08c9ddf0115d1118c7a00c04fc297eb01000000b9fe0ca15a2ffb4a9e172d76a87afae40000000002000000000003660000c000000010000000875eab73cc326c4acb70b609f170da6d0000000004800000a0000000100000001efe13d29355bea97995960f306c0009180000005a906fee5e408ccf685bbc56dd1c3e3c91472d168c17d38a140000008bf9a9f060d1c46d1d96441d2080218ff0ada1a6"

Otherwise:
To rename use:
$computername = Read-Host -Prompt "Enter computer name $computername"
$domain = "myDomain"
$user = "myUserAccount"
$password = Read-Host -Prompt "Enter password for $user" -AsSecureString
$credential = "$domain\$user"
Rename-computer –newname $computername –domaincredential $credential –force

To add it in AD:
$credentialObject = New-Object System.Management.Automation.PSCredential($user,$password)
Add-Computer -DomainName $domain -Credential $credentialObject
SeanAuthor Commented:
Cristian ,
Thank you. Will try it and provide feedback asap.
Sean
A. Cristian CsikiSenior System AdministratorCommented:
Sean,

Re-reading the sample code, I found a mistake:
$password = Read-Host -Prompt "Enter password for $user" -AsSecureString

Thinking faster than I type :)

Correct the line in:
$password = Read-Host -Prompt "Enter password for $password" -AsSecureString
Webinar: Cyber Crime Becomes Big Business

The rising threat of malware-as-a-service is not one to be overlooked. Malware-as-a-service is growing and easily purchased from a full-service cyber-criminal store in a “Virus Depot” fashion. Join us in our upcoming webinar as we discuss how to best defend against these attacks!

Nagendra Pratap SinghDesktop Applications SpecialistCommented:
You can delegate the domain join permissions to a normal account on a particular OU.
This is safer.

If the account you are using above is a domain admin then you are at risk even with an encrypted password.
SeanAuthor Commented:
Nagendra,

trying your suggestion also. Will provide feedback.
Thank you very much
Sean
SeanAuthor Commented:
Cristian,
Have some issues that I need to work out with your script. I am looking for a script that would only ask for the new computer name. The domain credentials should already be in the script. Would be nice to have the password encrypted  even-though I am using the "Delegate Control" in AD as Nagendra kindly suggested.

Thanks agian
Sean
A. Cristian CsikiSenior System AdministratorCommented:
Hi,

Test this code:

$computername = Read-Host -Prompt "Enter computer name $computername"
$domain = "myDomain"
$user = "myUserAccount"
$password = "myPassword" -AsSecureString //already encrypted
$mycredential = "$domain\$user"
Rename-computer –newname $computername –domaincredential $mycredential –force

To add it in AD:
$credentialObject = New-Object System.Management.Automation.PSCredential($user,$password)
Add-Computer -DomainName $domain -Credential $credentialObject 

Open in new window


Should request the input of computer name and have already other details preconfigured.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SeanAuthor Commented:
Hi Cristian,

Does not like  "//already encrypted"  

Thank you for your time
Sean
A. Cristian CsikiSenior System AdministratorCommented:
Hi Sean,

Remove that :-) It was a note for you.

Regards
SeanAuthor Commented:
Very sorry for not responding sooner. Some fires that need to be put out first. Will update as soon as possible.
Thanks
Sean
SeanAuthor Commented:
Thank you Cristian.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.