ryanmaves
asked on
Using the New-ADUser cmdlet -Name parameter not working with a variable input
Hi, please help me with my script below. I have set a couple of variables $Fname and $Lname and are using them to fill out most of the New-ADUser cmdlet parameters for a new user account. However, it seems New-ADUser -Name $Fname $Lname doesn't work because it thinks $Lname is an argument instead of part of the string input, any ideas?
Below is the code and the error,
Thanks
Below is the code and the error,
Thanks
function New-CompanyUser {
param (
[string]$Fname = (Read-Host 'Enter new user First Name only'),
[string]$Lname = (Read-Host 'Enter new user Last Name only'),
[string]$Description = 'New User Account',
[string]$Password = (Read-Host 'Enter a valid password for new user account')
)
Begin {
# Get OU Users
$OUU = "OU=Users,OU=FolderIT,OU=Folder,DC=Domain,DC=Domain"
$OUU
}
Process {
$check = Get-ADUser -Filter {Name -like $Name} -SearchBase $OUU
$check
if ($check) {
write-host "The user name $Name already exist"; break
}
else {
write-host "Creating a new user account $Name"
}
# Add new user to OU Users
$newUser = New-ADUser -Name $Fname $Lname -GivenName $Fname -Surname $Lname -Description $Description -DisplayName $Fname $Lnam -Enabled $true -SamAccountName ($Fname + '.' + $Lname) -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -ChangePasswordAtLogon $false -Path $OUU -PassThru
}
End{}
}
New-ADUser : A positional parameter cannot be found that accepts argument 'Last'.
At line:31 char:20
+ $newUser = New-ADUser -Name $Fname $Lname -GivenName $Fname -Surname $Ln ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : InvalidArgument: (:) [New-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsof t.ActiveDi rectory.Ma nagement.C ommands.Ne wADUser
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Viola, you have saved the day. To think I spent over an hour trying to figure this out, I actually did the "" over the variables at one point but concluded it didn't work. What I had forgot was to put "" over the Displayname parameter...so it looked like the same error and I incorrectly assumed it didn't work. ha ha
Thank you for the lesson!
Thank you for the lesson!
Not a problem.
Glad to help!
Will.
Glad to help!
Will.
ASKER