Help with Powershell script

param([string]$newpcname = (Read-Host 'Please enter new Computer name'))
Add-Computer -NewName $newpcname -DomainName test.ad.domain.com -Credential ad\domainadmin "OU=Test Machines, OU=Test Services, OU=Test LBs, OU=All LBs, DC=test, DC=ad, DC=domain, DC=com" -Restart

when I try this script i get the following error:

Add-Computer : Computer 'NewPC-test1' failed to join domain 'test.ad.domain.com' from its current workgroup 'TEST' with following error message: Access is denied.
At C:\Users\testuser\Desktop\Untitled2.ps1:4 char:1
+ Add-Computer -NewName $newpcname -DomainName test.ad.domain.com -Cred ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (NewPC-test1:String) [Add-Computer], InvalidOperationException
    + FullyQualifiedErrorId : FailToJoinDomainFromWorkgroup,Microsoft.PowerShell.Commands.AddComputerCommand

Can someone help me with this? Thanks!
Lou614IT ProAsked:
Who is Participating?
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.

Timothy McCartneySYS ADMINISTR I INFRASCommented:
The error indicates that the user running the script doesn't have the appropriate level of access.

Either you don't have local admin (administrative access for the workstation) or you don't have permissions to add machines to the domain.
I would verify that you are in the Administrators group on the computer, and that you are able to add computers to the domain manually.
0
Lou614IT ProAuthor Commented:
Hi Timothy, thank you for your reply. I do have local admin access to the PC and am able to add computers to the domain manually...
0
Timothy McCartneySYS ADMINISTR I INFRASCommented:
Could you try this?

param(
    [string]$newpcname = $(Read-Host 'Please enter new Computer name'),
    [pscredential]$creds = (Get-Credential)
)
Add-Computer -NewName $newpcname -DomainName test.ad.domain.com -Credential $creds -OUPath "OU=Test Machines,OU=Test Services,OU=Test LBs,OU=All LBs,DC=test,DC=ad,DC=domain,DC=com" -Restart 

Open in new window

0

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
Lou614IT ProAuthor Commented:
Tim, I appreciate you getting back to me but had already figured it out but . Your answer works. Thank you Sr.
2
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
Powershell

From novice to tech pro — start learning today.