-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sync-SympaMailingList.ps1
91 lines (75 loc) · 2.96 KB
/
Sync-SympaMailingList.ps1
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
function Sync-SympaMailingList
{
<#
.Synopsis
This function adds a member(s) to a Mailing list
.EXAMPLE
Sync Mailing list(s) against the file at C:\Sympa\synclist.csv
Sync-SympaMailingList -Sympa $Sympa -Path C:\Sympa\synclist.csv
#>
param(
[Parameter(Mandatory=$true,HelpMessage="Pass in the result of the 'Get-SympaLogin' function")]
$Sympa,
[Parameter(Mandatory=$true,HelpMessage="Enter the path to the reference file for the Mailing lists")]
[String]$Path
<#
[Parameter(Mandatory=$false,HelpMessage="Should you notify the user that they are being added to the list, default is no")]
[ValidateSet("Yes", "No")]
[String]$Notify = "No"
#>
)
<#
#Handle the $Notify paramater converting it into the mess that Sympa understands
switch ($Notify)
{
'Yes' {$Alert = "false"}
'No' {$Alert = "true"}
Default {$Alert = "true"}
}
#>
Process{
#Import the list
#Check (and asorb) that the file is really there, stop the script if it isn't
try
{
$Reference = Import-Csv $Path -ErrorAction Stop
}
catch
{
throw "Reference file not found"
}
#Convert the CSV into the same format that the results from Sympa will be coming out in
#Create empty collection
$ReferenceArray = New-Object System.Collections.ArrayList
#Build the data for the collection
foreach($Row in $Reference){
#Build an object to store the results in
$Item = New-Object -TypeName System.Object
#Add the members to the object
$Item | Add-Member -MemberType NoteProperty -Name "MailingList" -Value $Row.MailingList
$Item | Add-Member -MemberType NoteProperty -Name "Member" -Value $Row.Member
#Add the object to the collection
$ReferenceArray.Add($Item) | Out-Null
}
#Get only the unique Mailing lists
$UniqueLists = $Reference | Select-Object -Unique -Property MailingList
#Read the current members of those lists
$ResultsArray = foreach($UniqueList in $UniqueLists){
Get-SympaMailingListMember -Sympa $Sympa -MailingList $UniqueList.MailingList
}
#Compare against the referance list and build the To Do List
$ToDoList = Compare-Object -ReferenceObject $ReferenceArray -DifferenceObject $ResultsArray -Property MailingList, Member
if($ToDoList.Count -eq "0"){
Write-Verbose "Nothing to do"
}
#Perform Add/Remove
foreach($ToDo in $ToDoList){
if($ToDo.SideIndicator -eq "=>"){
Remove-SympaMailingListMember -Sympa $Sympa -MailingList $ToDo.MailingList -Member $ToDo.Member
}
elseif($ToDo.SideIndicator -eq "<="){
Add-SympaMailingListMember -Sympa $Sympa -MailingList $ToDo.MailingList -Member $ToDo.Member
}
}
}
}