New-AddUser Script Error

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: 

February 22nd, 2015 10:31am

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 head

Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 10:54am

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:

http://blogs.msdn.com/b/powershell/archive/2009/01/02/how-and-why-to-use-splatting-passing-switch-parameters.aspx

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.

February 22nd, 2015 11:48am

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.

-

Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 12:16pm

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?
February 22nd, 2015 1:00pm

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.

Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 1:07pm

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.

February 22nd, 2015 1:10pm

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.
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 2:30pm

Here are many resources to get you started

https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

February 22nd, 2015 4:19pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics