Link to home
Start Free TrialLog in
Avatar of skbarnard
skbarnard

asked on

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)
Avatar of SubSun
SubSun
Flag of India image

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

Avatar of skbarnard
skbarnard

ASKER

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
Can you post the script and input .csv file which you are using?
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.
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..
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
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!