"The format of the specified domain name is invalid" error in Powershell Add-Computer

Achievement_First
Achievement_First used Ask the Experts™
on
Hello Experts,
Need some help.  I'm building a Windows 7 image and need to run a Powershell script to automatically join the domain.  I first wanted to store the credential password to join the domain (see CTcharger's comments at http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_25218688.html?sfQueryTermInfo=1+10+30+7+domain+join+powershel+window).  But when trying to actually join the computer to the domain I get as error that states "The format of the specified domain name is invalid".  Here are the steps I've taken in powershell:
#Set-myCredential.ps1
$Credential = Get-Credential (I type in the credentials it asks me for)
$credential.Password | ConvertFrom-SecureString | Set-Content c:\windows\system32\windowspowershell\v1.0\password.txt
#Get-myCredential.ps1
$password = Get-Content c:\windows\system32\windowspowershell\v1.0\scriptpass.txt | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential "CachedUser",$password

After that I try to join it to the domain by typing in:
add-computer -domainname af.org -credential $credential -passthru

And that's when I get this error:
Add-Computer : This command cannot be executed on target computer ('ComputerName') due to the following error: The format of the specified domain name in invalid.
At line:1 char:13

If I type in the add-computer command manually putting in the credentials, it will join.  But cannot do it with the stored passwords.  What's up with that???
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
See this:
“Correct” way to programatically join a Windows 7 machine to the domain
http://serverfault.com/questions/154397/correct-way-to-programatically-join-a-windows-7-machine-to-the-domain

Author

Commented:
dj_alik

Thanks for the suggestion, but do you know why the Powershell commands that I type in don't work.
Also, my goal is to have this work automatically when the Administrator logs in over a wireless network.  Is it possible to put a command in, let's say, the runonce registry?

Author

Commented:
Where can I download netdom.exe for the 64bit version of Windows 7 Enterprise?
Figured it out.  I was typing in the actual word 'CachedUser' instead of the username used to join the computer to the domain.
So instead of typing in:
$credential = New-Object System.Management.Automation.PsCredential "CachedUser",$password

I typed in:
$credential = New-Object System.Management.Automation.PsCredential "domain\username",$password

And it worked.

Author

Commented:
Found the error in the command string I was typing.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial