Hello,
I have been tasked with constructing a mailer that will send staff information to certain people, based on their location and their roles. For instance:
$teamA = @()
$teamB = @()
$teamC = @()
$teamD = @()
$users = Import-CSV 'path_to_file.csv'
ForEach ($user in $users) {
#DeptA
if (user.department -like 'deptA*' -and user.title -match "jobA") {$teamA += "Username: $($user.username)`r`nTitle: $($user.title)`r`nLocation: $($user.location)`r`nOther Stuff: $($user.stuff)"}
if (user.department -like 'deptA*' -and user.title -match "jobB") {$teamB += "Username: $($user.username)`r`nTitle: $($user.title)`r`nLocation: $($user.location)`r`nOther Stuff: $($user.stuff)"}
#and so on...
#DeptB
if (user.department -like 'deptB*' -and user.title -match "jobA") {$teamC += "Username: $($user.username)`r`nTitle: $($user.title)`r`nLocation: $($user.location)`r`nOther Stuff: $($user.stuff)"}
if (user.department -like 'deptB*' -and user.title -match "jobB") {$teamD += "Username: $($user.username)`r`nTitle: $($user.title)`r`nLocation: $($user.location)`r`nOther Stuff: $($user.stuff)"}
#and so on...
#now the mailer...
$body = @"
`r`n
New Staff Accounts:
REPLACE
Do not reply to this email address.
Thank you!
"@
$email = @{
smtpserver = "myserver"
from = "New_Staff@my.org"
subject = "New Staff Stuff"
}
if (-not [string]::IsNullOrEmpty($teamA)) {
Send-MailMessage @email -body $body.Replace("REPLACE",$teamA) -To "user.guy@email.com"
}
if (-not [string]::IsNullOrEmpty($teamB)) {
Send-MailMessage @email -body $body.Replace("REPLACE",$teamB) -To "user.guy@email.com","user.otherguy@email.com"
}
}
The thing is, I have 19 departments. Not just a and b shown here. Add on to this that…
$teamA will be emailed to personA
$teamB will be emailed to person a AND b
$teamC will be emailed to person c
$teamD will be emailed to person d AND e
This is going to be long script of many, many If statements…unless there is a better way?
Thank you for any input.