What do you think of my spiritual thought reminder PowerShell script?

Some discussions just don't fit into a well defined box. Use this forum to discuss general topics and issues revolving around the Church and the technology offerings we use and share.
Post Reply
ataylor32
New Member
Posts: 13
Joined: Wed Apr 20, 2011 9:18 pm

What do you think of my spiritual thought reminder PowerShell script?

#1

Post by ataylor32 »

Like many wards, the ward I go to begins each leadership meeting with a spiritual thought. When I first became ward clerk, I noticed that people (myself included) were constantly forgetting that they had been assigned to give the spiritual thought. We all had copies of the spiritual thought calendar, yet we still forgot. Because of this, we always had to wing it.

To fix this situation, I decided to create a PowerShell script that e-mails a reminder to whoever is assigned to give the spiritual thought the next Sunday. After I created the script, I created a scheduled task to run it every Wednesday at 7:00 PM. Since then, it is extremely rare that anybody forgets that they have been assigned to give the spiritual thought.

I'm curious to know what others think of this script. The code appears below.

This is the command to run the script as a scheduled task:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe &\"C:\Users\John Doe\Documents\Spiritual Thought Reminder.ps1\"

Feel free to use the code and do whatever you want with it.

Code: Select all

Param(
    [String]$SmtpServer            = "smtp.gmail.com",
    [Int]$SmtpPort                 = 587,
    [Bool]$SmtpEnableSsl           = $True,
    [String]$SendEmailFromAddress  = "wardclerk@gmail.com",
    [String]$SendEmailFromName     = "Ward Clerk",
    [String]$SendEmailFromPassword = "clerkpassword",
    [Bool]$CCEmailToBishop         = $True
)

$Positions = @{
    "Bishop"                       = @{"Bishop Doe"    = "address@example.com"}
    "Bishopric 1st Counselor"      = @{"Brother Doe"   = "address@example.com"}
    "Bishopric 2nd Counselor"      = @{"Brother Doe"   = "address@example.com"}
    "Ward Clerk"                   = @{"Brother Doe "  = "address@example.com"}
    "Ward Executive Secretary"     = @{"Brother Doe"   = "address@example.com"}
    "High Priest Group Leader"     = @{"Brother Doe"   = "address@example.com"}
    "Elders Quorum President"      = @{"President Doe" = "address@example.com"}
    "Ward Mission Leader"          = @{"Brother Doe"   = "address@example.com"}
    "Relief Society President"     = @{"Sister Doe"    = "address@example.com"}
    "Young Men President"          = @{"Brother Doe"   = "address@example.com"}
    "Young Women President"        = @{"Sister Doe"    = "address@example.com"}
    "Primary President"            = @{"Sister Doe"    = "address@example.com"}
    "Sunday School President"      = @{"Brother Doe"   = "address@example.com"}
}

$Meetings = @{
    "2011-05-22" = @{
        "bishopric" = "Bishopric 1st Counselor"
        "priesthood executive committee" = "Elders Quorum President"
    }
    "2011-05-29" = @{
        "bishopric" = "Bishopric 2nd Counselor"
        "ward council" = "Relief Society President"
    }
}

### YOU SHOULD NOT EDIT THE CODE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING ###

Switch (Get-Date -Format ddd) {
    Sun {$DaysToAdd = 7; Break}
    Mon {$DaysToAdd = 6; Break}
    Tue {$DaysToAdd = 5; Break}
    Wed {$DaysToAdd = 4; Break}
    Thu {$DaysToAdd = 3; Break}
    Fri {$DaysToAdd = 2; Break}
    Sat {$DaysToAdd = 1; Break}
}

$NextSunday = (Get-Date).AddDays($DaysToAdd)
$NextSundayMachine = $NextSunday.ToString("yyyy-MM-dd")
$NextSundayFriendly = $NextSunday.ToLongDateString()

If ($Meetings.ContainsKey($NextSundayMachine)) {
    ForEach ($Meeting In @($Meetings[$NextSundayMachine].Keys)) {
        $Position = $Meetings[$NextSundayMachine][$Meeting]

        If ($Positions.ContainsKey($Position)) {
            ForEach ($SendEmailToName In @($Positions[$Position].Keys)) {
                $SendEmailToAddress = $Positions[$Position][$SendEmailToName]

                $EmailMessage       = New-Object System.Net.Mail.MailMessage
                $EmailMessage.From  = New-Object System.Net.Mail.MailAddress $SendEmailFromAddress, $SendEmailFromName
                $EmailMessage.To.Add((New-Object System.Net.Mail.MailAddress $SendEmailToAddress, $SendEmailToName))

                $BishopCCd = $False

                If ($CCEmailToBishop -Eq $True -And $Position -Ne "Bishop" -And $Positions.ContainsKey("Bishop")) {
                    ForEach ($BishopName In @($Positions["Bishop"].Keys)) {
                        $BishopAddress = $Positions["Bishop"][$BishopName]

                        Try {
                            $EmailMessage.CC.Add((New-Object System.Net.Mail.MailAddress $BishopAddress, $BishopName))

                            $BishopCCd = $True
                        }
                        Catch {
                            Write-Error "An error occurred adding $BishopName to the e-mail's `"CC`" field."
                        }
                    }
                }

                $EmailMessage.Subject = "Reminder: Spiritual Thought for $NextSundayFriendly"

                $EmailMessage.Body = @"
Hi $SendEmailToName,

This is a reminder that you have been assigned to share a spiritual thought at the $Meeting meeting on $NextSundayFriendly.

Thanks!
"@

                Try {
                    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SmtpPort)
                    $SMTPClient.EnableSsl = $SmtpEnableSsl
                    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SendEmailFromAddress, $SendEmailFromPassword)
                    $SMTPClient.Send($EmailMessage)

                    Write-Host -NoNewline "An e-mail was successfully sent from $SendEmailFromAddress to $SendEmailToAddress."

                    If ($BishopCCd -Eq $True) {
                        Write-Host " $BishopName was CC'd."
                    }
                    Else {
                        Write-Host
                    }

                    Write-Host
                }
                Catch {
                    Write-Error "An error occurred sending an e-mail from $SendEmailFromAddress to $SendEmailToAddress."
                }
            }
        }
        Else {
            Write-Error "Unable to e-mail the person responsible for sharing the spiritual thought at the $Meeting meeting on $NextSundayFriendly because their position wasn't found in the `"Positions`" hash table."
        }
    }
}
Else {
    Write-Host "There do not appear to be any meetings on $NextSundayFriendly."
}
Post Reply

Return to “General Discussions”