• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2124
  • Last Modified:

Script to create bulk email accounts

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
skbarnard
Asked:
skbarnard
  • 4
  • 4
1 Solution
 
SubsunCommented:
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
 
skbarnardAuthor Commented:
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
 
SubsunCommented:
Can you post the script and input .csv file which you are using?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
skbarnardAuthor Commented:
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
 
SubsunCommented:
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
 
skbarnardAuthor Commented:
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
 
SubsunCommented:
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
 
skbarnardAuthor Commented:
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!
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now