-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-file-for-constant-contact.ps1
116 lines (80 loc) · 3.47 KB
/
create-file-for-constant-contact.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
$file1 = "C:\<your path here>\book1.csv"
$file2 = "C:\<your path here>\book2.csv"
$file3 = "C:\<your path here>\book3.csv"
$exportPath = "C:\<your path here>\book4.csv"
# Import files
$csv1 = Import-Csv $file1
$csv2 = Import-Csv $file2
$csv3 = Import-Csv $file3
# Create array of all email addresses
$emailArray = @()
foreach ($_ in $csv1) {$emailArray += $_.email}
foreach ($_ in $csv2) {$emailArray += $_.email}
foreach ($_ in $csv3) {$emailArray += $_.email}
# Keep only unique email addresses
$emailArray = ($emailArray | Select-Object -Unique)
# Compare Array and CSVs and create tags
$objectArray = @()
foreach ($address in $emailArray) {
foreach ($record in $csv1) {
if ($address -eq $record.email) {
if ($address -in $objectArray.email) {
$index = $objectArray.email.IndexOf($address)
if ($objectArray[$index].tags -notmatch $record.grade) {
#add grade to tags
$objectArray[$index].tags = $objectArray[$index].tags + ", " + $record.grade
} else {}
if ($objectArray[$index].tags -notmatch $record.school) {
#add school to tags
$objectArray[$index].tags = $objectArray[$index].tags + ", " + $record.school
} else {}
} else {
$a = "" | select email, tags
$a.email = $address
$a.tags = $record.grade + ", " + $record.school
$objectArray += $a
}
} else {}
}
foreach ($record in $csv2) {
if ($address -eq $record.email) {
if ($address -in $objectArray.email) {
$index = $objectArray.email.IndexOf($address)
if ($objectArray[$index].tags -notmatch $record.grade) {
#add grade to tags
$objectArray[$index].tags = $objectArray[$index].tags + ", " + $record.grade
} else {}
if ($objectArray[$index].tags -notmatch $record.school) {
#add school to tags
$objectArray[$index].tags = $objectArray[$index].tags + ", " + $record.school
} else {}
} else {
$a = "" | select email, tags
$a.email = $address
$a.tags = $record.grade + ", " + $record.school
$objectArray += $a
}
} else {}
}
foreach ($record in $csv3) {
if ($address -eq $record.email) {
if ($address -in $objectArray.email) {
$index = $objectArray.email.IndexOf($address)
if ($objectArray[$index].tags -notmatch $record.grade) {
#add grade to tags
$objectArray[$index].tags = $objectArray[$index].tags + ", " + $record.grade
} else {}
if ($objectArray[$index].tags -notmatch $record.school) {
#add school to tags
$objectArray[$index].tags = $objectArray[$index].tags + ", " + $record.school
} else {}
} else {
$a = "" | select email, tags
$a.email = $address
$a.tags = $record.grade + ", " + $record.school
$objectArray += $a
}
} else {}
}
}
$objectArray | Export-Csv -Path $exportPath -NoTypeInformation