Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, March 25, 2020 9:28 PM
###########
#Sample 1
###########
$FirstName = "Bob"
$LastName = "Bobertson"
$newUserParams = @{
Name = ("{0} {1}" -f $FirstName, $LastName)
Path = "OU=Test1,OU=Test2,OU=Test3,OU=Test4,OU=Test5,DC=Everything,DC=local"
}
try {
$newUser = New-ADUser $newUserParams
}
catch {
"There was an issue creating {0} {1}. {2}" -f $user.LastName, $user.FirstName, $_
}
##New-ADUser: Access is denied
###########
#Sample 2
###########
$FirstName = "Bob"
$LastName = "Bobertson"
$newUserParams = @{
Name = ("{0} {1}" -f $FirstName, $LastName)
}
try {
$newUser = New-ADUser $newUserParams -Path "OU=Test1,OU=Test2,OU=Test3,OU=Test4,OU=Test5,DC=Everything,DC=local"
}
catch {
"There was an issue creating {0} {1}. {2}" -f $user.LastName, $user.FirstName, $_
}
##New-ADUser: The name provided is not a properly formed account name
###########
###########
I am very new to the power shell and I want to understand the mistake I am doing in the above code.
In the Sample # 1 - I got "access is denied"
In the Sample # 2 - I got " The name provided is not a properly formed account name"
I do have the "manage user privilege and I am able to create the new-aduser with the below code.
$First_Name = "Bob"
$Last_Name = "Bobertson"
$Sam_Account_Name = "{0}{1}" -f $First_Name.Substring(0,1), $Last_Name
$Friendly_Name = ("{0} {1}" -f $First_Name, $Last_Name)
$Job_Title = "Accounting Analyst I"
$AccountPassword = "P@ssword1" | ConvertTo-SecureString -AsPlainText -Force
$newUsrParams = @{
Name = $Friendly_Name
Title = $Job_Title
GivenName =$First_Name
Surname = $Last_Name
DisplayName = $Friendly_Name
SamAccountName = $Sam_Account_Name
UserPrincipalName = "{0}@mycompany.com" -f $Sam_Account_Name
AccountPassword = $AccountPassword
Enabled = $true
ChangePasswordAtLogon = $true
ErrorAction = "Stop"
Path = "OU=Test1,OU=Test2,OU=Test3,OU=Test4,OU=Test5,DC=Everything,DC=local"
}
try {
$newUser = New-ADUser @newUsrParams
}
catch {
"There was an issue creating {0}. {1}" -f $Sam_Account_Name, $_
}
Please advise. TIA.
All replies (7)
Wednesday, March 25, 2020 10:13 PM ✅Answered | 1 vote
To splat a param set use "@".
New-ADUser @newUserParams
You didn't do that in your first two examples.
\(ツ)_/
Wednesday, March 25, 2020 10:03 PM
Please post the full error message and not jsut the text. That tells us what is malformed and where.
\(ツ)_/
Thursday, March 26, 2020 12:42 AM
thank you for the reply.
I would like to know how can I get a full error message. Currently, I am calling the script in the console and that is the only text being returned.
Thursday, March 26, 2020 12:47 AM
thank you much. it worked perfect.
Thursday, March 26, 2020 12:47 AM
You are not returning the error but are translating it into text.
Use "Throw $_" to return the error. Changing it into text makes it unusable.
\(ツ)_/
Thursday, March 26, 2020 12:56 AM
I have "$_" in the catch block but, for some reason it is not going into catch block.
I tried to run the sample # 1 including additional parameters "telephoneNumber = $user.WorkPhone"
I was hoping for the catch block to throw error message with error description but, I got the below message.
"New-ADUser: A parameter cannot be found that matches parameter name 'telephoneNumber'."
but, I have that as one of the attribute in the AD. please advise. thank you.
Thursday, March 26, 2020 5:25 PM
Thank you, Richard.