New-IEXMailboxFromCsv


posted by ps1
09-23-2009

Downloads: 270
File size: 2.9kB
Views: 566

Embed
New-IEXMailboxFromCsv
  1. ## ===================================================================== 
  2. ## Title       : New-IEXMailboxFromCsv 
  3. ## Description : Create mailbox using data from CSV file. 
  4. ## Author      : Idera 
  5. ## Date        : 09/15/2009 
  6. ## Input       :  New-IEXMailboxFromCsv [[-CsvFilePath] <String>] [-ResetPasswordOnNextLogon] [-WhatIf] [-Confirm] 
  7. ##    
  8. ## Output      : Microsoft.Exchange.Data.Directory.Management.Mailbox 
  9. ## Usage       : 
  10. ##               1. Check what would happen if you try to create mailboxes using c:\users.csv as input file. 
  11. ##               New-IEXMailboxFromCsv -CsvFilePath c:\users.csv -WhatIf 
  12. ## 
  13. ##               2. Create mailboxes using data from c:\users.csv, set "User must change password at next log on", and ask for confirmation. 
  14. ##               New-IEXMailboxFromCsv -CsvFilePath c:\users.csv -ResetPasswordOnNextLogon -Confirm             
  15. ##                              
  16. ## Notes       : 
  17. ## Tag         : Exchange 2007, mailbox, create 
  18. ## Change log  : 
  19. ## ===================================================================== 
  20.  
  21. ## sample CSV file 
  22. # Name,SamAccountName,Password,FirstName,LastName,Database,OrganizationalUnit 
  23. # Test1,Test1,P@ssw0rd,Test,User1,Mailbox Database,"domain.com/users" 
  24. # Test2,Test2,P@ssw0rd,Test,User2,Mailbox Database, 
  25. # Test3,Test3,P@ssw0rd,Test,User3,Mailbox Database,"cn=users,dc=domain,dc=com" 
  26.  
  27.  
  28. #requires -pssnapin Microsoft.Exchange.Management.PowerShell.Admin  
  29.  
  30. function New-IEXMailboxFromCsv 
  31. {  
  32.  
  33.     param
  34.     [string]$CsvFilePath = $(Throw "Parameter 'CsvFilePath' cannot be empty"), 
  35.     [switch]$ResetPasswordOnNextLogon
  36.     [switch]$WhatIf
  37.     [switch]$Confirm 
  38.     
  39.  
  40.     trap
  41.         Write-Error $_ 
  42.         continue 
  43.     
  44.  
  45.     if(Test-Path -Path $CsvFilePath -PathType Leaf) 
  46.     
  47.  
  48.         $DomainFQDN = ([ADSI]"").distinguishedName -replace 'dc=' -replace ',','.' 
  49.  
  50.         Import-Csv -Path $CsvFilePath | Foreach-Object
  51.             $line = $_ 
  52.  
  53.             if(!$_.OrganizationalUnit) {   
  54.                 Write-Warning "OU name was not specified, '$DomainFQDN/Users'  will be used ins." 
  55.                 $_.OrganizationalUnit = "$DomainFQDN/Users"  
  56.             
  57.  
  58.             $db = Get-MailboxDatabase | Where-Object {$_.Name -eq $line.Database
  59.  
  60.             if(!$db
  61.             
  62.                 Throw "Database '$Database' could not be found" 
  63.             
  64.  
  65.             $Pwd = ConvertTo-SecureString -String $_.Password -AsPlainText -Force 
  66.  
  67.             $UPN = "{0}@$DomainFQDN" -f $_.name 
  68.  
  69.             New-Mailbox -Name $_.Name -FirstName $_.FirstName -LastName $_.LastName -SamAccountName $_.SamAccountName -Password $Pwd -Database $db -UserPrincipalName $UPN -OrganizationalUnit $_.OrganizationalUnit -ResetPasswordOnNextLogon:([bool]$ResetPasswordOnNextLogon) -WhatIf:$WhatIf -Confirm:$Confirm 
  70.         
  71.     
  72.     else 
  73.     
  74.         Throw "File: '$CsvFilePath' cannot be found" 
  75.     

Create mailbox using data from CSV file.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.