Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $dcs = 'dc1.domain1.company.local','dc2.domain2.company.local'
- $ErrorCount = 0
- $DC2match = "^The operation couldn't be performed because object '.*' couldn't be found on 'dc1\.domain1\.company\.local'\.$"
- $DC1match = "^The operation couldn't be performed because object '.*' couldn't be found on 'dc2\.domain2\.company\.local'\.$""
- $firstname = Read-Host -Prompt 'Enter First Name'
- $lastname = Read-Host -Prompt 'Enter Last Name'
- $alias = $firstname.substring(0,1).tolower()+$lastname.tolower()
- $primarysmtpaddress = $alias+'@company.com'
- foreach ($dc in $dcs)
- {
- try {
- $Mailbox = Get-Recipient -DomainController $dc -Identity $PrimarySmtpAddress -ErrorAction Stop
- } catch {
- $MailboxError = $_.Exception.Message
- $MailboxErrorRegex = "^The operation couldn't be performed because object '.*' couldn't be found on '.*'\.$"
- if ($MailboxError -match $MailboxErrorRegex) {
- $ErrorCount++
- }
- }
- }
- if ($ErrorCount -eq 2) {
- $pw = Read-Host -Prompt "Enter Password" -AsSecureString
- $db = Read-Host -Promp "Enter Database"
- $name = $firstname+' '+$lastname
- $upn = $alias+'@domain1.company.local'
- $x400 = 'X400:C=US;A= ;P=Company;O=OU;S='+$lastname+';G='+$firstname+';'
- $identity = 'domain1.company.local/OU/OU/'+$name
- $secondsmtpaddress = 'smtp:'+$alias+'@secondsmtpaddress.com'
- 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
- }
- elseif ($Error[0].Exception.Message -match $DC2match) {
- Write-Host Exists on Domain2
- }
- elseif ($Error[0].Exception.Message -match $DC1match) {
- Write-Host Exists on Domain1
- }
- if ($ErrorCount -eq 2) {
- Set-Mailbox -EmailAddressPolicyEnabled $false -EmailAddresses $primarysmtpaddress,$secondsmtpaddress,$x400 -Identity $identity
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement