Solved

Script to create bulk email accounts

Posted on 2013-01-11
8
1,171 Views
Last Modified: 2013-02-01
We're beginning a program at my work where we're deploying a bunch of iPads.  Each iPad needs an associated email box so I'd like to know if anyone has a script that will read a .CSV file and create a mailbox with the information provided in that .CSV file. ("bulk" mailbox creation)
0
Comment
Question by:skbarnard
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 38768912
Try This..
Import-CSV C:\test.csv | ForEach-Object { New-Mailbox -Lastname $_.LastName -Name $_.Name -FirstName $_.FirstName -Organization "test.com/Admin Purpose" -Database "DB001" -UserPrincipalName $_.UserPrincipalName -Password (ConvertTo-SecureString $_.password -AsPlainText -Force)}

Open in new window

CSV format
Lastname,Firstname,Name,UserPrincipalName,Password
Test,User,TestUser,TestUser@test.com,P@ssw0rd

Open in new window

0
 

Author Comment

by:skbarnard
ID: 38778871
I keep getting an error that the fields have null values.  I've removed a couple of fields that were throwing the error knowing I can go to the account once it's created and add them back in but then one of the other fields becomes problematic (null or empty string)
I've removed all the fields I possibly can from the CSV file and still receive the attached error.
BulkMailboxError.pdf
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38779685
Can you post the script and input .csv file which you are using?
0
 

Author Comment

by:skbarnard
ID: 38779847
It's basically a tweak from what you provided above.  Does the order matter?  I created the csv file in this order Name, First, Last, Alias, UserPrincipal, Organization, Database, Password.
I received errors first on the Name field (the name has a space in it) so I just took that field out of the csv and out of the script.
I also took the Database and Organization fields out of the csv and hard-coded them in the script and completely removed Alias.
The remaining fields in the csv file are First, Last, UserPrincipal and Password.
I've included the code as I used it
Import-CSV C:\temp\Last-Hope-iPad-Accounts.csv | ForEach-Object { New-Mailbox -FirstName $._FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -Organization "iPad User Accounts" -Database DB9 -Password $_.password -AsPlainText -Force }
(it also threw an error when I had the "ConvertTo-SecureString" so I removed that as well)
As a final try, I removed the -AsPlainText -Force from the script and it complains that the UserPrincipalName is an empty string (see the attached file from my last post)
That same error is thrown each time I remove a field from the csv and script.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 40

Expert Comment

by:Subsun
ID: 38779897
Order doesn't matter but if you are using following header for CSV file the script also should change based on headers..
Name, First, Last, Alias, UserPrincipal, Organization, Database, Password
It should be like..
Import-CSV C:\temp\Last-Hope-iPad-Accounts.csv | ForEach-Object { New-Mailbox -FirstName $._First -LastName $_.Last -UserPrincipalName $_.UserPrincipal -Organization "iPad User Accounts" -Database $_.Database -Password (ConvertTo-SecureString $_.Password -AsPlainText -Force)}

Open in new window

If you still have issue please post the csv file which you are using. I think something is wrong with the file format..
0
 

Author Comment

by:skbarnard
ID: 38828699
I ended up creating all the accounts "manually" - there weren't that many left when I posted this question.
However, it would be nice to know what I did wrong so I've attached the CSV file I was attempting to use.
I had no header row in this file - can't remember why it was left off.  The format is:
first name, last name, alias, userprincipalname, password.
Last-Hope-iPad-Accounts.csv
0
 
LVL 40

Accepted Solution

by:
Subsun earned 300 total points
ID: 38829815
If your CSV file don’t have a header then Import-csv will consider the first line of CSV file as a header, and script will fail..

You can also specify the header in script by adding Header  parameter to Import-CSV command.. for example..

Import-CSV C:\temp\Last-Hope-iPad-Accounts.csv -Header firstname,lastname,alias,userprincipalname,password | ForEach-Object { New-Mailbox -FirstName $._First -LastName $_.Last -UserPrincipalName $_.userprincipalname -Organization "iPad User Accounts" -Database $_.Database -Password (ConvertTo-SecureString $_.Password -AsPlainText -Force)}

Open in new window

0
 

Author Closing Comment

by:skbarnard
ID: 38844886
While I haven't had a chance to retry this script, not having a header on the import csv file is likely what caused the script not to work.
I am keeping a copy of this script for the next time I have many accounts to create.
Thanks Subsun!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video discusses moving either the default database or any database to a new volume.

744 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

12 Experts available now in Live!

Get 1:1 Help Now