Advertisement
Guest User

CheckCreate

a guest
Nov 14th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $dcs = 'dc1.domain1.company.local','dc2.domain2.company.local'
  2. $ErrorCount = 0
  3. $DC2match = "^The operation couldn't be performed because object '.*' couldn't be found on 'dc1\.domain1\.company\.local'\.$"
  4. $DC1match = "^The operation couldn't be performed because object '.*' couldn't be found on 'dc2\.domain2\.company\.local'\.$""
  5. $firstname = Read-Host -Prompt 'Enter First Name'
  6. $lastname = Read-Host -Prompt 'Enter Last Name'
  7. $alias = $firstname.substring(0,1).tolower()+$lastname.tolower()
  8. $primarysmtpaddress = $alias+'@company.com'
  9.  
  10. foreach ($dc in $dcs)
  11. {
  12.    
  13.     try {
  14.         $Mailbox = Get-Recipient -DomainController $dc -Identity $PrimarySmtpAddress -ErrorAction Stop
  15.  
  16. }   catch  {
  17.         $MailboxError = $_.Exception.Message
  18.         $MailboxErrorRegex = "^The operation couldn't be performed because object '.*' couldn't be found on '.*'\.$"
  19.    
  20.         if ($MailboxError -match $MailboxErrorRegex) {
  21.             $ErrorCount++
  22.             }
  23. }
  24. }  
  25.  
  26. if ($ErrorCount -eq 2) {
  27.     $pw = Read-Host -Prompt "Enter Password" -AsSecureString
  28.     $db = Read-Host -Promp "Enter Database"
  29.     $name = $firstname+' '+$lastname
  30.     $upn = $alias+'@domain1.company.local'
  31.     $x400 = 'X400:C=US;A= ;P=Company;O=OU;S='+$lastname+';G='+$firstname+';'
  32.     $identity = 'domain1.company.local/OU/OU/'+$name
  33.     $secondsmtpaddress = 'smtp:'+$alias+'@secondsmtpaddress.com'
  34.  
  35.     New-Mailbox -Name $name -Alias $alias -OrganizationalUnit 'domain1.company.local/OU/OU' -UserPrincipalName $upn -SamAccountName $alias -FirstName $firstname -Initials '' -LastName $lastname -Password $pw -ResetPasswordOnNextLogon $false -Database $db -PrimarySmtpAddress $primarysmtpaddress
  36. }
  37.    
  38.     elseif ($Error[0].Exception.Message -match $DC2match) {
  39.         Write-Host Exists on Domain2
  40.    }
  41.    
  42.     elseif ($Error[0].Exception.Message -match $DC1match) {
  43.         Write-Host Exists on Domain1
  44.    }
  45.    
  46. if ($ErrorCount -eq 2) {
  47.     Set-Mailbox -EmailAddressPolicyEnabled $false -EmailAddresses $primarysmtpaddress,$secondsmtpaddress,$x400 -Identity $identity
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement