Advertisement
timsstuff

Create-ResMailboxes.ps1

Oct 23rd, 2015
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.     [ValidateScript({Test-Path $_ -PathType Leaf})]
  4.     [Parameter(Mandatory=$True,Position=1)][string]$CSV
  5. )
  6.  
  7. Import-Module ActiveDirectory
  8. if(!(Get-PSSnapin |
  9.     Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
  10.       ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
  11. }
  12.  
  13. $start = Get-Date
  14. $domain = Get-ADDomain
  15. $addomain = $domain.Forest
  16. $domaindn = $domain.DistinguishedName
  17. $logfile = ".\CreateResMailboxes.log"
  18. $nl = "`r`n"
  19. $ou = "OU=General Mailboxes,OU=Exchange,$domaindn"
  20.  
  21. function writelog([string]$message) {
  22.     $timestamp = Get-Date
  23.     $fc = "White"
  24.     if($message.contains("ERROR")) {$fc ="Red"}
  25.     Add-content $logfile -value "$timestamp : $message"
  26.     Write-Host $message -ForegroundColor $fc
  27. }
  28.  
  29. writelog "Importing resource mailboxes from $csv at $start"
  30. Write-Host $nl
  31.  
  32. Import-Csv $csv | ForEach-Object {
  33.     Set-Variable -Name testmb -Value $null -Scope 0
  34.     $upn = $_.SamAccountName + "@" + $addomain
  35.     $name = $_.DisplayName
  36.     $alias = $_.Alias
  37.     $email = $_.PrimarySmtpAddress
  38.     $testmb = Get-Mailbox $alias -ErrorAction SilentlyContinue
  39.     if($testmb -eq $null) {
  40.         writelog "Creating mailbox $name ($email)"
  41.         New-Mailbox -name $name -DisplayName $name -Shared -Alias $alias -UserPrincipalName $upn -PrimarySmtpAddress $email -OrganizationalUnit $ou
  42.     }
  43.     else {
  44.         writelog "Mailbox  $name ($email) already exists, skipping."
  45.     }
  46. }
  47.  
  48. $end = Get-Date
  49. $elapsed = $end - $start
  50. Write-Host $nl
  51. writelog "Finished import at $end ($elapsed)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement