How can I configure the SAM to run a custom PowerShell script to gather event logs and send emails after the server reboot?
This is the script:
$Timestamp = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'India Standard Time')
$Text = "Timelines in IST"
$paramGetWinEvent1 = @{
FilterHashtable = @{ logname = 'Application' }
MaxEvents = 10
}
$paramGetWinEvent2 = @{
FilterHashtable = @{ logname = 'Security' }
MaxEvents = 10
}
$EmailBody = get-winevent @paramGetWinEvent1 | Format-List -Property TimeCreated, Message
$EmailBody += get-winevent @paramGetWinEvent2 | Format-List -Property TimeCreated, Message
$EmailFrom = "servermonitor@accenture.com"
$EmailTo = "dotnet-helpers@accenture.com"
$EmailSubject = "Server Login Notification"
$SMTPServer = "smtp.sendgrid.net"
[string][ValidateNotNullOrEmpty()]$Username = "azure_ad8e8e784erf789.com"
[string][ValidateNotNullOrEmpty()]$pwd = "xxxxxxxxxxx"
$pwd1 = ConvertTo-SecureString -String $pwd -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $pwd1
$SMTPPort = 587
$paramSendMailMessage = @{
From = $EmailFrom
To = $EmailTo
Subject = $EmailSubject
Body = ($EmailBody + "($Text)" + "($Timestamp)" | Out-String)
SmtpServer = $SMTPServer
Port = $SMTPPort
Credential = $cred
}
Send-MailMessage @paramSendMailMessage
Thank you.