#Copies ProxyAddresses, Telephone numbers and thumbnailPhotofrom AD to AD LDS and EmployeeNumber from AL LDS to AD.

param ([string]$UserList = $( Read-Host "Enter the name and path to the user list, please" )) #User list is a line delimited list of email addresses or usernames

$ErrorActionPreference = “Stop”

$a = new-object -comobject wscript.shell

$intAnswer = $a.popup("Do you want to run in Report Mode? (Data will not be written to AD or Relay)", 0,"Report Mode",4)

$Users = get-content "$UserList"

Remove-Item *.rpt

Remove-Item NotInRelay.*

$myData = @()

$NR = @()

foreach ($user in $Users)

{

$NID=""

$NFAD=""

$Found=$true #Used to mark User as not found in AD or Relay

$Name=$User.Trim() #remove any leading or trailing spaces.

If ($Name.contains("@")) #If it's in the form of an email address

{

$Name=$Name.split("@")

$User=$Name[0] #Revert to first.last format

}

else #Already is first.last format.

{

$User=$name

}

       If ($User.Length -le 2)#If the name is too short skip the ANR search.

       {

       $Temp=$Name[0]+"@"+$Name[1]

       $PSMTP = Get-Mailbox $temp | Select-Object "PrimarySmtpAddress","DistinguishedName" #Get the Primary SMTP address for the user

       }

       else

       {

       #Write-Host "Greater than 2"

       $PSMTP = Get-Mailbox -ANR $User | Select-Object "PrimarySmtpAddress","DistinguishedName" #Get the Primary SMTP address for the user

       }     

       if ($PSMTP -eq $Null) #Opps Not found in AD

       {

       $Found=$False #Found set to false

       Write-Host $user was skipped because the user was not found in AD! -ForegroundColor Red

       $ADNF +=1

       $NFAD="X"

       $user | Out-File NotInAD.rpt -Append

       $myData += New-Object -TypeName PSObject -Property @{

       "Name" = $Name[0]

       "PSMTP" = $RelayUserName

       "Employee ID" = ""

       "cruDesignation" = ""

       "Synced" = ""

       "Not Synced" = "X"

       "No AD" = $NFAD

       "No ID" = $NID} | Select-Object "Name","PSMTP","Synced","Not Synced","No AD","No ID","Employee ID","cruDesignation"      

       }     

       If ($Found)  

              {     

              Foreach ($Object in $PSMTP)

              {                   

              $RelayUserName = $Object.PrimarySmtpAddress.local + "@" + $Object.PrimarySmtpAddress.Domain #Builds Relay user name string

              #Write-Host "The object found for $User are" $PSMTP

              #Write-Host $RelayUserName , ($PSMTP.Count)" was found in AD."

              $RelayUserName | Out-File "SMTP.$UserList.rpt" -Append #This is written to the PrimarySMTP list from Active Directory list.

              $RelayTarget = Get-ADObject -Filter 'UserPrincipalName -eq $RelayUserName' -searchbase "CN=IDM,DC=cru,DC=org" -server 'RelayLdap.cru.org:389' -properties "employeeNumber","userPrincipalName","cruDesignation","cruJobCode","cruMinistryCode","cruSubMinistryCode" #Connect to Relay get Emp# & UPN

              #$RelayTarget | ft

              If (-not $RelayTarget -eq "") #If it's $Null then the object (User) was not found in relay.

                     {                    

                     $emailAddresses = Get-MailBox $RelayUserName -ea SilentlyContinue| select-object DistinguishedName -Expandproperty EmailAddresses | select Proxyaddressstring #Gets Proxy Addresses from Active Directory

                     for ($i = 0; $i -lt $emailAddresses.Length; $i += 1)

                           {

                                  if ($emailAddresses[$i] -Match 'smtp:' -and $intAnswer -eq 7)

                                  {

                                  Set-ADObject $RelayTarget -server 'RelayLdap.cru.org:389' -add @{proxyAddresses=$emailAddresses[$i].ProxyAddressString} #Add this proxyAddress to Relay

                                  #$emailAddresses[$i].ProxyAddressString

                                  }

                           }

                           $UserAttributes = Get-ADUser $Object.DistinguishedName -ErrorAction silentlyContinue -Properties "HomePhone","MobilePhone","OfficePhone","ipPhone","fax","thumbnailPhoto","givenName"

                           if ($UserAttributes.HomePhone -ne $null -and $intAnswer -eq 7){

                           Set-ADObject $RelayTarget -ErrorAction silentlyContinue -server 'RelayLdap.cru.org:389' -Replace @{homePhone=$UserAttributes.HomePhone}}

                           if ($UserAttributes.MobilePhone -ne $null -and $intAnswer -eq 7){

                           Set-ADObject $RelayTarget -ErrorAction silentlyContinue -server 'RelayLdap.cru.org:389' -Replace @{mobile=$UserAttributes.MobilePhone}}

                           if ($UserAttributes.OfficePhone -ne $null -and $intAnswer -eq 7){

                           Set-ADObject $RelayTarget -ErrorAction silentlyContinue -server 'RelayLdap.cru.org:389' -Replace @{TelephoneNumber=$UserAttributes.OfficePhone}}

                           if ($UserAttributes.ipPhone -ne $null -and $intAnswer -eq 7){

                           Set-ADObject $RelayTarget -ErrorAction silentlyContinue -server 'RelayLdap.cru.org:389' -Replace @{ipPhone=$UserAttributes.ipPhone}}

                           if ($UserAttributes.Fax -ne $null -and $intAnswer -eq 7){

                           Set-ADObject $RelayTarget -ErrorAction silentlyContinue -server 'RelayLdap.cru.org:389' -Replace @{facsimileTelephoneNumber=$UserAttributes.Fax}}

                           if ($UserAttributes.thumbnailPhoto -ne $null -and $intAnswer -eq 7){

                           Set-ADObject $RelayTarget -ErrorAction silentlyContinue -server 'RelayLdap.cru.org:389' -Replace @{thumbnailPhoto=$UserAttributes.thumbnailPhoto}}                           

                           #Write the Employee ID back to Active Directory.

                           if ($UserAttributes.givenName -ne $null -and $intAnswer -eq 7){

                           Set-ADObject $RelayTarget -ErrorAction silentlyContinue -server 'RelayLdap.cru.org:389' -Replace @{cruPreferredName=$UserAttributes.givenName}}                              

                           #Write the Employee's first name to the Realy cruPreferredName attribute.

                           try

                           {

                           $EmpID = $RelayTarget.employeeNumber

                           $cruDesg = $RelayTarget.cruDesignation

                           If($cruDesg -eq $Null){$cruDesg=" EMPTY "}

                           Set-ADObject $UserAttributes -Replace @{employeeID=$EmpID} #Write Employee ID from Relay to Active Directory.

                           $Conjunction = "has"

                           }

                           Catch #$EmpID must be Null

                           {

                           Write-Host "$RelayUserName's employee ID is not in Relay.(NOTE: Still Added to the updated users list)" -BackgroundColor Yellow -ForegroundColor Black

                           $Conjunction = "Has NOT"

                           If($EmpID -eq $Null){$EmpID=" EMPTY "}

                           $name=$RelayUserName.split("@")

                           #Write-Host "$RelayUserName's cruDesignation: $cruDesg"

                           $RelayUserName + "   Employee ID:" + $EmpID + "   cruDesignation:" + $cruDesg | Out-File NoID.rpt -Append

                           #$myData += New-Object -TypeName PSObject -Property @{

                           #"Name" = $Name[0]

                           #"Employee ID" = $EmpID

                           #"cruDesignation" = $cruDesg

                           #"In Relay" = ""

                           #"Not In Relay" = ""

                           #"No ID" = "X"} | Select-Object "Name","In Relay","Not In Relay","No ID","Employee ID","cruDesignation"

                           $NID="X"

                           $NoID+=1

                           }                         

                     }

                     Else

                     {

                     Write-Host $RelayUserName was skipped because the user was not found in Relay! -ForegroundColor Yellow

                     $Found=$False

                     $RNF +=1

                    

                     $name=$RelayUserName.split(".")

                     $NR += New-Object -TypeName PSObject -Property @{

            "Name" = $Name[0]

                     "E-mail" = $RelayUserName} | Select-Object "Name","E-mail"

                    

                     $name=$RelayUserName.split("@")

                     $myData += New-Object -TypeName PSObject -Property @{

              "Name" = $Name[0]

                           "PSMTP" = $RelayUserName

                           "Employee ID" = ""

                           "cruDesignation" = ""

                           "Synced" = ""

                           "Not Synced" = "X"

                           "No AD" = $NFAD

                           "No ID" = $NID} | Select-Object "Name","PSMTP","Synced","Not Synced","No AD","No ID","Employee ID","cruDesignation"                              

                     $RelayUserName | Out-File NotInRelay.rpt -Append

                     }                                                     

              }            

              }                         

if ($Found)

{                   

$count+=1

$RelayUserName | Out-File AD_and_RelaySynced.rpt -Append

$name=$RelayUserName.split("@")

$myData += New-Object -TypeName PSObject -Property @{

"Name" = $Name[0]

"PSMTP" = $RelayUserName

"Employee ID" = $EmpID

"cruDesignation" = $cruDesg

"Synced" = "X"

"Not Synced" = ""

"No AD" = $NFAD

"No ID" = $NID} | Select-Object "Name","PSMTP","Synced","Not Synced","No AD","No ID","Employee ID","cruDesignation"

}

}

write-host $ADNF "Not found in AD" -ForegroundColor red

write-host $RNF "Not found in Relay" -ForegroundColor red

write-host $Noid "Without ID #'s in Relay" -ForegroundColor red

write-host $count "Users updated" -ForegroundColor Green

$myData | Export-Csv Report.csv -notype

$NR| Export-Csv NotInRelay.csv -notype

dir *.rpt