Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Scripting Issue

Posted on 2013-06-14
4
Medium Priority
?
1,880 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 43

Accepted Solution

by:
Adam Brown earned 2000 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 41

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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

972 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