falconcurt
asked on
Import CSV Bulk Users Powershell
I am attempting to import users from a CSV file. I have knowledge with VB but not with Powershell. I found a script that will work but im not sure it will work with other AD attributes like title, or homeDirectory. I see the New-Mailbox command which there are no parameters that will work for title or homeDirectory. If anybody can help me plug this in. Would be awesome. Thanks!
Function ReadCSV{
Param([string]$fileName)
$users = Import-Csv $fileName
foreach ($user in $users){
$ht = @{
'givenName'=$user.fn
'sn'=$user.ln
'displayName'=$user.dispna me
'alias'=$user.alias
'company'=$user.company
'description'=$user.descri ption
'samAccountName'=$user.ali as
'userPrincipalName' = $user.upn
'database' = $user.Database
'organizationalUnit' = $user.ou
'name' = ($user.fn + $user.sn)
}
Write-Output $ht
}
}
Function CreateUser{
Param($userInfo)
New-Mailbox -Name $userInfo['name' ]`
-Alias $userInfo['alias'] -UserPrincipalName $userInfo['userPrincipalNa me'] `
-SamAccountName $userInfo['alias'] -Database $userInfo['Database']`
-FirstName $userInfo['givenName'] -LastName $userInfo['sn'] `
-OrganizationalUnit $userinfo['organizationalU nit']`
-DisplayName $userInfo['DISPLAYNAME'] -Password (ConvertTo-SecureString $_.password -AsPlainText -Force)
}
Function CreateMailbox{
PROCESS
{
CreateUser $_
}
}
ReadCSV c:\Temp\UsersPS.csv | CreateMailbox
Function ReadCSV{
Param([string]$fileName)
$users = Import-Csv $fileName
foreach ($user in $users){
$ht = @{
'givenName'=$user.fn
'sn'=$user.ln
'displayName'=$user.dispna
'alias'=$user.alias
'company'=$user.company
'description'=$user.descri
'samAccountName'=$user.ali
'userPrincipalName' = $user.upn
'database' = $user.Database
'organizationalUnit' = $user.ou
'name' = ($user.fn + $user.sn)
}
Write-Output $ht
}
}
Function CreateUser{
Param($userInfo)
New-Mailbox -Name $userInfo['name' ]`
-Alias $userInfo['alias'] -UserPrincipalName $userInfo['userPrincipalNa
-SamAccountName $userInfo['alias'] -Database $userInfo['Database']`
-FirstName $userInfo['givenName'] -LastName $userInfo['sn'] `
-OrganizationalUnit $userinfo['organizationalU
-DisplayName $userInfo['DISPLAYNAME'] -Password (ConvertTo-SecureString $_.password -AsPlainText -Force)
}
Function CreateMailbox{
PROCESS
{
CreateUser $_
}
}
ReadCSV c:\Temp\UsersPS.csv | CreateMailbox
ASKER
I will also need to Add-Mailbox.
Looks like the function within DSADD only created the address for LDAP queries and not the physical mailbox, see if this is of any help:
http://www.computerperform ance.co.uk /vbscript/ vbscript_u ser_mailbo x.htm
http://www.computerperform
ASKER
I would like to stick with the Powershell method if possible. If i do not find anything after a while.... I will look into that method.
ASKER
I know the parameters of powershell but there are none for certain attributes like title and homeDirectory. There inlies my problem.
Can't you use DSADD to create the user accounts with the parameters you need and a VBscript to create the mailboxes?
ASKER
I believe a powershell session has to be called in order to create a mailbox within Exchange 2010. I'm not positive but believe so. I know you can create a mailbox using Vbscript withing 2003 exchange but i do not believe it is possible within 2010. Just my first thoughts doing a quick Google search.
Hi,
This powershell code will create bulk mailbox. Once it done, it will modify the user's atribute such title, company, office...
To run this code you must have Exhcnage PSSnapin Microsoft.Exchange.Managem ent.PowerS hell.Admin and Microsoft.Exchange.Managem ent.Powers hell.Suppo rt or else just on using Exchange Management PowerShell.
Please let verify the code.
Test.csv
This powershell code will create bulk mailbox. Once it done, it will modify the user's atribute such title, company, office...
To run this code you must have Exhcnage PSSnapin Microsoft.Exchange.Managem
$Users = Import-Csv C:\PS\CreateMailboxSC.csv
#Create a bulk mailboxes
Foreach ($User in $Users){
#Create a Mail-Enabled User
New-Mailbox -Name $User.name -FirstName $User.firstname -LastName $User.lastname -Alias $User.alias `
-UserPrincipalName ($user.alias +'@contoso.com') -Database $User.Database -OrganizationalUnit $User.OU `
-Password ( ConvertTo-SecureString P@ssw0rd -AsPlainText -Force) -ResetPasswordOnNextLogon:$false `
-ErrorVariable err | Out-Null
#Check AD Latency
do
{
$mailbox = Get-Mailbox -Identity $user.alias -ErrorAction SilentlyContinue
}
While ($mailbox -eq $null)
#Modify attributes of User's account properties
Set-User -Identity $user.alias -Company $user.Company -Title $user.title -Department $user.department `
-Office $User.office -Manager $user.manager
}
Please let verify the code.
Test.csv
ASKER
It looks good. I haved only one question. I see you have a set password in the script. How would i implement a unique password for each user within the CSV file.
ASKER
I actually see another problem... There are still some parameters that are missing even with the Set-User command like homeDirectory. I see some stuff related to QADUser command. Has anybody worked with this before?
ASKER
Ok I finally got something that may work. I reworked the script. I went with Set-Qaduser commands on editing the attributes that i could not find with the other methods. Now my only problem is attempting to set the Member of attribute for each user withing the CSV. I am looking to use the -ObjectAttributes parameter but i do not know the correct usage with that part. Anybody have a clue on how to implement that part.
Here is my script.
add-PSSnapin Microsoft.Exchange.Managem ent.PowerS hell.E2010 , Quest.ActiveRoles.ADManage ment
$Users = Import-Csv C:\temp\UsersPS.csv
#Create a bulk mailboxes
Foreach ($User in $Users){
$Group = $User.
#Create a Mail-Enabled User
New-Mailbox -Name $User.name -FirstName $User.givenName -LastName $User.sn -Alias $User.alias `
-UserPrincipalName ($user.alias +'domain.loc') -Database $User.Database -RetentionPolicy MBSPolicy -OrganizationalUnit $User.OU `
-Password ( ConvertTo-SecureString $User.password -AsPlainText -Force) -ResetPasswordOnNextLogon: $false `
-ErrorVariable err | Out-Null
Add-MailboxPermission -Identity $user.alias -User "Mailbox Administrators" -AccessRights FullAccess -Automapping $false
#Check AD Latency
do
{
$mailbox = Get-Mailbox -Identity $user.alias -ErrorAction SilentlyContinue
}
While ($mailbox -eq $null)
#Modify attributes of User's account properties
Set-QADUser -Identity $user.alias -Description $user.description -Memberof -Company $user.Company -Title $user.Title -HomeDirectory $user.homeDirectory `
-Office $User.office -HomeDrive $user.Homedrive -ObjectAttributes -PasswordNeverExpires:$tru e
Add-QADPermission -Identity $user.alias -Account SELF,Everyone -Extendedright "User-Change-Password" -Deny -ApplyTo ThisObjectOnly
}
Here is my script.
add-PSSnapin Microsoft.Exchange.Managem
$Users = Import-Csv C:\temp\UsersPS.csv
#Create a bulk mailboxes
Foreach ($User in $Users){
$Group = $User.
#Create a Mail-Enabled User
New-Mailbox -Name $User.name -FirstName $User.givenName -LastName $User.sn -Alias $User.alias `
-UserPrincipalName ($user.alias +'domain.loc') -Database $User.Database -RetentionPolicy MBSPolicy -OrganizationalUnit $User.OU `
-Password ( ConvertTo-SecureString $User.password -AsPlainText -Force) -ResetPasswordOnNextLogon:
-ErrorVariable err | Out-Null
Add-MailboxPermission -Identity $user.alias -User "Mailbox Administrators" -AccessRights FullAccess -Automapping $false
#Check AD Latency
do
{
$mailbox = Get-Mailbox -Identity $user.alias -ErrorAction SilentlyContinue
}
While ($mailbox -eq $null)
#Modify attributes of User's account properties
Set-QADUser -Identity $user.alias -Description $user.description -Memberof -Company $user.Company -Title $user.Title -HomeDirectory $user.homeDirectory `
-Office $User.office -HomeDrive $user.Homedrive -ObjectAttributes -PasswordNeverExpires:$tru
Add-QADPermission -Identity $user.alias -Account SELF,Everyone -Extendedright "User-Change-Password" -Deny -ApplyTo ThisObjectOnly
}
ASKER
Here i go again. I found what i need to add to a group. I added the Add-ADGroupmember command to the script. Now one more problem. lol.. How do i combine two fields that have the first name and last name within the CSV file. I am trying to reduce the amount of columns that are needed within the CSV file. Here is what i got. Tell me what am i missing.
dd-PSSnapin Microsoft.Exchange.Managem ent.PowerS hell.E2010 , Quest.ActiveRoles.ADManage ment
Import-Module ActiveDirectory
$Users = Import-Csv C:\temp\UsersPS1.csv
#Create a bulk mailboxes
Foreach ($User in $Users){
#Create a Mail-Enabled User
New-Mailbox -Name ("$User.sn, $User.givenName") -FirstName $User.givenName -LastName $User.sn -Alias $User.alias `
-UserPrincipalName ($user.alias +'@domain.loc) -Database $User.Database -RetentionPolicy MBSPolicy -OrganizationalUnit $User.OU `
-Password ( ConvertTo-SecureString $User.password -AsPlainText -Force) -ResetPasswordOnNextLogon: $false `
-ErrorVariable err | Out-Null
Add-MailboxPermission -Identity $user.alias -User "Mailbox Administrators" -AccessRights FullAccess -Automapping $false
#Check AD Latency
do
{
$mailbox = Get-Mailbox -Identity $user.alias -ErrorAction SilentlyContinue
}
While ($mailbox -eq $null)
#Modify attributes of User's account properties
Set-QADUser -Identity $user.alias -Description $user.alias -Company $user.Company -Title $user.Title -HomeDirectory $user.homeDirectory `
-Office $User.office -HomeDrive G: -PasswordNeverExpires:$tru e
Add-QADPermission -Identity $user.alias -Account SELF,Everyone -Extendedright "User-Change-Password" -Deny -ApplyTo ThisObjectOnly
Add-ADGroupMember -Identity $user.Group -Member $user.alias
dd-PSSnapin Microsoft.Exchange.Managem
Import-Module ActiveDirectory
$Users = Import-Csv C:\temp\UsersPS1.csv
#Create a bulk mailboxes
Foreach ($User in $Users){
#Create a Mail-Enabled User
New-Mailbox -Name ("$User.sn, $User.givenName") -FirstName $User.givenName -LastName $User.sn -Alias $User.alias `
-UserPrincipalName ($user.alias +'@domain.loc) -Database $User.Database -RetentionPolicy MBSPolicy -OrganizationalUnit $User.OU `
-Password ( ConvertTo-SecureString $User.password -AsPlainText -Force) -ResetPasswordOnNextLogon:
-ErrorVariable err | Out-Null
Add-MailboxPermission -Identity $user.alias -User "Mailbox Administrators" -AccessRights FullAccess -Automapping $false
#Check AD Latency
do
{
$mailbox = Get-Mailbox -Identity $user.alias -ErrorAction SilentlyContinue
}
While ($mailbox -eq $null)
#Modify attributes of User's account properties
Set-QADUser -Identity $user.alias -Description $user.alias -Company $user.Company -Title $user.Title -HomeDirectory $user.homeDirectory `
-Office $User.office -HomeDrive G: -PasswordNeverExpires:$tru
Add-QADPermission -Identity $user.alias -Account SELF,Everyone -Extendedright "User-Change-Password" -Deny -ApplyTo ThisObjectOnly
Add-ADGroupMember -Identity $user.Group -Member $user.alias
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had to research all my answers myself.
dsadd user cn=USERNAME,cn=Users,dc=DO
http://www.tekweb.dk/manua