Issue

a mail needs to be sent to specific users on a specific Vault event using powerEvents


Solution

powerEvents is able to hook up to specific Vault Events. You can choose your preferred event and send an email when the event is triggered. In this example an email is sent when a file is released.


Register the event

With this code you can register the event to that the function gets executed every time the event is triggered. The EventName defines the event while -Action defines the function that is executed when the event is triggered.

Register-VaultEvent -EventName UpdateFileStates_Post -Action FileReleased


This is the function that gets executed when a Vault file changes state to released. At the beginning information about the E-Mail gets collected and then an E-Mail is sent with Send-MailMessage. 

Function FileReleased ($files) {
    foreach ($file in $files) {
        if ($file._State -eq "Released") {
            $sender = "sender@yourcompany.com"
            $receivers = @("receiver@yourcompany.com", "receiver2@yourcompany.com")
            $subject = "Subject"
            $body = "Write your email text here and use variables to add additional information"
            $smtp = "yoursmtp"
            $passwd = ConvertTo-SecureString -AsPlainText "Password" -Force #Insert your password
            $cred = new-object Management.Automation.PSCredential $sender, $passwd
            $attachments = @() #Fill with absolute filepaths to add attachments to the mail. E.g. @("C:\TEMP\testfile.txt", "C:\TEMP\testfile2.txt")
            $useSSL = $false

            foreach($address in $receivers) {
                if($attachments.Count -gt 0) {
                    Send-MailMessage -From $sender -To $receivers -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -Attachments $attachments -UseSsl:$useSSL
                }
                else {
                    Send-MailMessage -From $sender -To $receivers -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -UseSsl:$useSSL
                }
            }   
        }
    }
}


Troubleshooting

If you get following error message:


Change $useSLL = $false to:

$useSSL = $true


See Also

Send-MailMessage (Microsoft Docs)

powerEvents (coolOrange Wiki)