1

I wanted to create a script to create a User through it in Active Directory. I already created a one-liner, which creates the User correctly. But know I want the script to create a User with different inputs.

I created the code, but somehow it doesn't work, can anybody elaborate what I did wrong?

$name=$args[0]
$givenname=$args[1]
$surname=$args[2]
$userlogin=$args[3]
$loginold=$args[4]
$description=$args[5]
$phone=$args[6]
$mail=$args[7]
$password=$args[8]
$passwordexpire=$args[9]
$locked=$args[10]
$oupath=$args[11]

echo $name
echo $givenname
echo $surname
echo $userlogin
echo $loginold
echo $description
echo $phone
echo $mail
echo $password
echo $passwordexpire
echo $locked
echo $oupath

New-ADUser -Name $name -GivenName $givenname -Surname $surname -UserPrincipalName $userlogin -SamAccountName $loginold -description $description -OfficePhone $phone -EmailAddress $mail -AccountPassword (Read-Host -AsSecureString "AccountPassword") $password -PasswordNeverExpires $passwordexpire -Enabled $locked -path $oupath

pause

Error message:

New-ADUser : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\POwershell\Create_ad_user.ps1:27 char:18
+ New-ADUser -Name $name -GivenName $givenname -Surname $surname -UserPrincipalNam ...
+                  ~~~~~
    + CategoryInfo          : InvalidData: (:) [New-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADUser

Press Enter to continue...: 
PS C:\Users\Administrator\Desktop>
2
  • does echo $name output something? Also, -AccountPassword doesn't look right. Commented Nov 28, 2016 at 15:08
  • 2
    Can you show us how you are calling the script from the command line? Also please take a look at Powershell Parameters they make things much easier than using $args Commented Nov 28, 2016 at 15:11

1 Answer 1

2

Looks like AccountPassword isn't correct as you are prompting for it (Read-Host -AsSecureString "AccountPassword") then supplying it in $password directly afterwards.

When you've got a large amount of parameters to pass splatting is often a good idea as it makes everything much easier to read like so:

$Params = @{
    Name = $args[0]
    Givenname = $args[1]
    Surname = $args[2]
    UserPrincipalName = $args[3]
    SamAccountName = $args[4]
    Description = $args[5]
    OfficePhone = $args[6]
    EmailAddress = $args[7]
    AccountPassword = ConvertTo-SecureString -AsPlainText $args[8] -Force
    PasswordNeverExpires = $args[9]
    Enabled = $args[10]
    Path = $args[11]
}

Write-Output $Params

New-ADUser @Params
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you fo your answer, and sorry for the big delay. When trying your code, i get the following error: ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null. At C:\POwershell\create_ad_user_eingabe_test1.ps1:10 char:59 + AccountPassword = ConvertTo-SecureString -AsPlainText $args[8] -Force + ~~~~~~~~ seems like something with the Password is wrong again to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.