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
Sunday, February 22, 2015 3:28 PM
I'm hoping someone can help me with the following New-AddUser script, which works in my test environment, but does not work in my production environment. I get the following output, after running the script: I also included the script that produces the output
cmdlet New-ADUser at command pipeline position 1
Supply values for the following parameters:
Name:
PS C:\Windows\system32> import-csv 'x:\bsed.csv' | ` ForEach-Object { `
new-aduser -GivenName $_.'First name' `
-Surname $_.'Last name' `
-UserPrincipalName ($_.'Principle Name' + "@ds.detroitmi.gov") `
-SamAccountName $_.'SAM Account' `
-DisplayName $_.'Display name' `
-EmailAddress $_.'E-mail' `
-OfficePhone $_.'Phone' `
-Department $_.'Depart' `
-Company "City of Detroit" `
-streetAddress $_.'Address' `
-City "Detroit" `
-State "Michigan" `
-Office $_.'Office Name' `
-Description $_.'Job title' }
parameters:
Name:
All replies (8)
Sunday, February 22, 2015 3:51 PM ✅Answered
Not likely that this script works anywhere which is why you don't believe the error message.
"Name" is a required parameter. You have not specified name.
Here is a more normal way to do this and avoids all of the backticks.
Fill in the name and it should work:
import-csv x:\bsed.csv |
ForEach-Object{
$uprops=@{
Name=??????????????????
GivenName=$_.'First name'
Surname=$_.'Last name'
UserPrincipalName=($_.'Principle Name' + '@ds.detroitmi.gov')
SamAccountName=$_.'SAM Account'
DisplayName=$_.'Display name'
EmailAddress=$_.'E-mail'
OfficePhone=$_.'Phone'
Department=$_.'Depart'
Company='City of Detroit'
streetAddress=$_.'Address'
City='Detroit'
State='Michigan'
Office=$_.'Office Name'
Description=$_.'Job title'
}
new-aduser @uprops
}
Avoid using backticks as they are almost always unnecessary and will cause you some terrible headaches.
¯\(ツ)_/¯
Sunday, February 22, 2015 4:44 PM ✅Answered
Indeed I agree with the method jrv posted, this is called splatting and can be used to organize parameters cmdlets. There is a nice post by the PowerShell team about how and why to use this:
In general splatting is preferred over backticks in PowerShell, the reason for this is that when you accidentally put a space behind the backtick your entire script can fail. Splatting is a more rigid system of passing on parameters to cmdlets while still being able to organize it in a readable format.
Jaap Brasser
http://www.jaapbrasser.com
Sunday, February 22, 2015 5:13 PM
Good explanation by Jaap. One other thing is that the backticks are very hard to see and can fail oddly when used incorrectly.
The biggest deficiency with the scripts is really that it is missing the mandatory "Name" argument. It is critical that all using PowerShell learn to pay close attention to the errors. In many systems errors are very terse. Maybe only a number. In PowerShell the errors are very helpful and point at specific issues. Often the error will include suggestions on how to remedy the situation.
-
¯\(ツ)_/¯
Sunday, February 22, 2015 5:56 PM
I'm thankful for all of the replies, and please forgive me for my newbies questions. I don't clearly understand what should go alone with the "Name=??????" will this represents the user's full name?
Sunday, February 22, 2015 6:04 PM
I'm thankful for all of the replies, and please forgive me for my newbies questions. I don't clearly understand what should go alone with the "Name=??????" will this represents the user's full name?
This has nothing to do with PowerShell or scripting. It is an Active Directory requirement. The catch-22 of Windows is that you need to understand the technology you are working with.
Every object in AD has CN value which is known as "Name" under ADSI. Every object must be named. A user account has a "pre-Windows 2000" name called a SamAccountName which is optional but it also has a "Name". THat name needs to be Unique. It is recommended that you sue the SamAccountName as it must be unique.
I would recommend spending some time learning the basics of AD before attempting to script AD. This wi prevent confusion and disasters.
¯\(ツ)_/¯
Sunday, February 22, 2015 6:07 PM
Just to note. The minimum requirement for creating a user is the CN value.
New-AdUser -Name mynewuser
Note that setting name with no SAmAccountName set s SAM to the same as "Name" so be sure that "Name" is unique if you want it to default.
¯\(ツ)_/¯
Sunday, February 22, 2015 7:27 PM
Hip Hip Hooray, I added a variable field for Name= and now it's working fine. Thanks everyone. As a Newbie, can anyone give me a good site or book for improving my skills.
Sunday, February 22, 2015 9:16 PM
Here are many resources to get you started
https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx
¯\(ツ)_/¯