how to create powershell script to add user to AD that will allow variables.

I have a script to add a user account to AD but I need it to be accomplished with variables.  This is my script.

New-ADUser -SamAccountName blah -Name "blah" -AccountPassword (ConvertTo-SecureString -AsPlainText "somePassword" -Force) -Enabled $true -PasswordNeverExpires $true -Path 'CN=Users,DC=slacker,DC=jeans,DC=net'

The issue I am having is with the Path.   I need to use variable to do this because the domain will not be the same all the time.   it might be slacker.jeans.net or it could be taco.burrito.net    I need to have users be able to plug in the variable and run the script to add the user account.  

I created  some variables.

# API | parameter | Domain | label: Domain | default-value: slacker
# API | parameter | 2Domain | label: 2Domain | default-value:jeans
# API | parameter | 3Domain | label: 3Domain | default-value: net
# API | parameter | Users | label: Users | default-value: CN=Users
# API | gap
# API END

param (
    $Domain,
    $2Domain,
    $3Domain,
    $Users
)


This allows users to put in the variables they want.   if they are not in the slacker.jeans.net domain they can change it and run the script to add the user.  

Here is my PS

New-ADUser -SamAccountName blah -Name "blah" -AccountPassword (ConvertTo-SecureString -AsPlainText "somePassword" -Force) -Enabled $true -PasswordNeverExpires $true -Path CN= $Users, DC=$Domain, DC=$2Domain, DC=$3Domain  -enabled $true



My problem is I cannot get the Path to take the variables.  it doesn't like syntax.   Any ideas how to do this?????
gmckfnAsked:
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.

footechCommented:
The -path parameter needs a string.  The following should work.

New-ADUser -SamAccountName blah -Name "blah" -AccountPassword (ConvertTo-SecureString -AsPlainText "somePassword" -Force) -Enabled $true -PasswordNeverExpires $true -Path "CN= $Users,DC=$Domain,DC=$2Domain,DC=$3Domain"  -enabled $true

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
gmckfnAuthor Commented:
is there a way to add the whole FQDN to the powershell script instead of putting in 3 parameters for the path??  

that way users can input jeans.taco.com on one line instead of using parameters for each DC=??
0
footechCommented:
You can do just about anything, but I don't really have a good idea of all the variations that you want to be able to submit and have handled, and/or where accounts should then be created.  This should work for creating accounts in the default Users container of the specified domain.
param (
     $Domain
 )
$pathDN = "CN=Users,DC=$($Domain -replace "\.",",DC=")"
New-ADUser -SamAccountName blah -Name "blah" -AccountPassword (ConvertTo-SecureString -AsPlainText "somePassword" -Force) -Enabled $true -PasswordNeverExpires $true -Path $pathDN  -enabled $true

Open in new window

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.