Solved

Powershell Scripting Issue

Posted on 2013-06-14
4
1,546 Views
Last Modified: 2013-06-14
Hi - I'm trying to create a powershell script to create new active directory users.  Attached is my CSV file and script.  I'm receiving the following errors:

-DisplayName : The term '-DisplayName' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:19 char:6
+                     -DisplayName $DisplayName -EmailAddress $Email -Description $Description`
+                     ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-DisplayName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-GivenName : The term '-GivenName' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:20 char:6
+                     -GivenName $FirstName -Surname $Surname -PassThru -Enabled $true -Path $OU`
+                     ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-GivenName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:21 char:6
+                     -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -force)`
+                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Please assist.
Book3.csv
script.txt
0
Comment
Question by:emeka57
4 Comments
 
LVL 38

Accepted Solution

by:
Adam Brown earned 500 total points
ID: 39248610
It looks like you have a carriage return or two in your new-aduser command. Remove those. Each command has to exist on a single line with no carriage returns.
0
 

Author Comment

by:emeka57
ID: 39248622
Did that and still receiving the same errors.  See below:

+                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-DisplayName : The term '-DisplayName' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:19 char:6
+                     -DisplayName $DisplayName -EmailAddress $Email -Description $Description`
+                     ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-DisplayName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-GivenName : The term '-GivenName' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:20 char:6
+                     -GivenName $FirstName -Surname $Surname -PassThru -Enabled $true -Path $OU`
+                     ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-GivenName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:21 char:6
+                     -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -force)`
+                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39248769
I can see lot of trailing space in your csv file, which probably causing the error..

Is the users listed in CSV got created without any issue?

Try this code...
Import-Module ActiveDirectory
Import-Module MSOnline
Set-ExecutionPolicy Unrestricted

	$Users = Import-Csv Book3.csv
	ForEach ($User in $Users)
	{
		$OU = $User.Path #"OU=Faculty,OU=Canarsie ACS (LS),DC=ascendlearning,DC=org" 
		$Name = $User.Name
		$GivenName = $User.FirstName
		$Surname = $User.LastName
		$SAM = $User.SAMAccountname
		$UPN = $User.UPN
		$Email = $User.EmailAddress
		$Description = $User.Description
                $Password = $User.Password
		$Group = $User.Group
		
		New-ADUser -Name $Name -SamAccountName $SAM -UserPrincipalName $UPN -DisplayName $Name -EmailAddress $Email -Description $Description -GivenName $GivenName -Surname $Surname -Enabled $true -Path $OU -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -force) -PassThru
		#Add-ADGroupMember -Identity $Group -Member $SAM
	}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 39248988
When I downloaded your CSV, I had to remove all the commas at the end of each line (actually just the header line was sufficient) in order for the import-csv to work.
When you use line continuation with backticks, typically you should have a space right before the backtick.  When I did that, and also correct "givenname" to "$givenname" in your new-aduser command, your script worked fine for me.
0

Join & Write a Comment

Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now