SyncADContacts.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function Sync-ADContact{
    [CmdletBinding()]
    param (
        [Parameter()]
        [String]
        $PathToImportFile,
        [String]
        $ADContatctsDName,
        [String]
        $ADContatctGroupsDName,
        [String]
        $SenderEmail,
        [String]
        $RecipientEmail,
        [String]
        $SMTPServer,
        [String]
        $EmailSubject
    )
    BEGIN{
        try {
            $All_OK = $false
            Write-Debug "Test if file exists"
            Write-Verbose "Test if file exists"
            if(Test-Path $PathToImportFile){
                Write-Debug "Importing contacts from file"
                $ImportedContacts = Import-CSV -Path $PathToImportFile â€“Delimiter ";"
                #NOTE "Use if you need to filter some contacts out"
                #$ImportedContacts = $ImportedContacts | Where-Object Mail -NotLike "*SomeThing*"
            }
            Write-Debug "Get AD conatcts"
            if(Get-ADOrganizationalUnit -Identity $ADContatctsDName){
                $ADContacts = Get-ADObject -Filter '(ObjectClass -eq "contact")' -SearchBase $ADContatctsDName -Properties *
            }
            Write-Debug "Get AD Contacts' groups"
            if(Get-ADOrganizationalUnit -Identity $ADContatctGroupsDName){
                $ADContatcGroups = Get-adgroup -Filter * -SearchBase $ADContatctGroupsDName -Properties *
            }
        }
        catch {
            $Message = ("The Task failed!!"+"<br/>"+"Please check the file or the OUs path"+"<br/>"+$Error[0])
            $htmlbody = "<html><body><font color='#008000'>$Message</font><br /><br /><body><html>"
            Send-MailMessage -From $SenderEmail -To $RecipientEmail -Subject $EmailSubject -Body $htmlbody -BodyAsHtml -SmtpServer $SMTPServer
            Exit
        }
    }
    PROCESS{
        try {
            foreach($ImportedContact in $ImportedContacts){
                $ImportedContactCN = ((($ImportedContact.Name).Trim()) + " " + (($ImportedContact.Surname).Trim()))
                if($ADContacts.CN -contains $ImportedContactCN){
                    Write-Verbose "Contact $ImportedContactCN exists in the AD"
                    $ADContact = $ADContacts | Where-Object CN -Like $ImportedContactCN
                    if(($ImportedContact.mail) -ne ($ADContact.Mail)){
                        $ContactMail = $ImportedContact.mail
                        Write-Output "Emails do not match, email will be updated with $ContactMail"
                        Set-ADObject $ADContact -Replace @{mail="$ContactMail"}
                    }
                }
                else{
                    Write-Output "Contact $ImportedContactCN does not exist in the AD, creating new contact"
                    $Params = @{
                        Name            = $ImportedContactCN
                        path            = "OU="+($($ImportedContact.company))+","+($ADContatctsDName) #Feel free to edit this to create the proper DN path for the proper OU
                        DisplayName     = $ImportedContactCN
                        Description     = $($ImportedContact.company) # Edit for the proper description
                        Type            = "contact"
                        OtherAttributes = @{'givenName'="$(($ImportedContact.Name).Trim())";
                            'sn'        ="$(($ImportedContact.Surname).Trim())";
                            'Company'   ="$(($ImportedContact.company).Trim())";
                            'mail'      ="$(($ImportedContact.mail).Trim())";
                        }
                    }
                    New-ADObject  @Params -Verbose;
                    $ADGroup = $ADContatcGroups | Where-Object CN -like "*$($ImportedContact.Company)*"
                    Write-Output "Adding $($ImportedContact.Name) to the AD group $($ADGroup.name)"
                    Set-ADGroup -Identity $($ADGroup.CN) -Add @{'member'="$($ADContact.DistinguishedName)"} -Verbose
                }
            }
            $ADContacts = $null
            $ADContacts = Get-ADObject -Filter '(ObjectClass -eq "contact")' -SearchBase $ADContatctsDName -Properties *
            foreach($ADContact in $ADContacts){
                if(($ImportedContacts.mail -notcontains ($($ADContact.mail)))){
                    Write-Warning "$($ADContact.name) no longer exists in the import list and will be deleted!!"
                    Set-ADObject -Identity $ADContact -ProtectedFromAccidentalDeletion $false
                    Remove-ADObject -Identity $ADContact -Confirm:$false
                }
            }
            $All_OK = $true
        }
        catch {
            $Message = ("The Task failed!!"+"<br/>"+"Something went wrong when attempting to create the contact or adding it to the group"+"<br/>"+$Error[0])
            $htmlbody = "<html><body><font color='#008000'>$Message</font><br /><br /><body><html>"
            Send-MailMessage -From $SenderEmail -To $RecipientEmail -Subject $EmailSubject -Body $htmlbody -BodyAsHtml -SmtpServer $SMTPServer
        }
    }
    END{
        if($All_OK){
            $Message = ("The Task Succedded!!"+"<br/>"+"Contacts are synced"+"<br/>"+$Error[0])
            $htmlbody = "<html><body><font color='#008000'>$Message</font><br /><br /><body><html>"
            Send-MailMessage -From $SenderEmail -To $RecipientEmail -Subject $EmailSubject -Body $htmlbody -BodyAsHtml -SmtpServer $SMTPServer
        }
    }
}