I wanted to start splitting up the string, and did so at the left parens. I indicated to the .Split() method to make three substrings, so I could get the date and time on a line by itself. For fun, try changing 3, to 1 and 2 to see the difference.
I was wondering if you could take a look at my code and let me know if I could have improved it? As I am just starting out, getting the syntax correct was my main hurdle here.
Any feedback would be appreciated.
#Read last line from the Log file
$Logfile ="C:\karl\ATM-E-DocsReceiptsproatm.log"
$Server ="Proclaim-ATM2"
$ATMName ="Edocs Reciepts ATM"
$to = "Karl@helphire.co.uk"
$from = "ATMMonitor@helphire.co.uk"
$subject = "Critical Message from node: $server"
$x = get-content $Logfile -tail 1
#If last line contains specific words exit the script
if ($x -match "FORCED SHUTDOWN" )
{
Write-host "Forced shutdown in Last Line"
Exit
}
else
{
#Cuts out the date field from the string
$logDateTime = ($x.split("(",3)[-1]).trimend(")")
#Comapres the current time to the Log date Time
$TimeDiff = New-TimeSpan -end $logDateTime
#If Time difference is greater than 5 Minutes Send email.
if ($TimeDiff.minutes -lt -5)
{
$email_body ="The $ATMName on server $Server has not written to its log file since $logDateTime and requires investigation
The log file can be found here: $logfile
This will trigger a Proclaim_emailalert."
send-mailmessage -to $to -from $from -subject $Subject -smtpserver "mail.internal.local" -body $email_body -bodyashtml;
}
}
Some Powershell functionality you should look into is here strings and splatting. Also, keep in mind you don’t need to use “Exit” because your if code block doesn’t do anything else if you meet the if criteria. Using Write-Host is also a bad practice. You can either just return a string or use [cmdletbinding()] and then you can use Write-Verbose to display information for trouble shooting using the -Verbose switch. Take a look at the below:
$Logfile ="C:\karl\ATM-E-DocsReceiptsproatm.log"
$Server ="Proclaim-ATM2"
$ATMName ="Edocs Reciepts ATM"
#check out Powershell splatting. We define all parameters in
#a hash table and then can pass those to Send-MailMessage
$splat = @{to = "Karl@helphire.co.uk";
from = "ATMMonitor@helphire.co.uk"
subject = "Critical Message from node: $server";
smtpserver = "mail.internal.local"
attachments=$Logfile
bodyashtml =$true
}
$content = get-content $Logfile -tail 1
#Cuts out the date field from the string
$logDateTime = ($content.split("(",3)[-1]).trimend(")")
#Check out Powershell here strings. Allows you to format
#something like HTML and pass it later. Comes in handy
#with SQL queries as well.
$hereString =@"
An email message from our sponsor
The $ATMName on server $Server has not written to its log file since $logDateTime and requires investigation
The log file is attached for review.
This will trigger a Proclaim_emailalert."
"@
#Dynamically add body parameter since body is ready
$splat.Add("body", $hereString)
#If last line contains specific words exit the script
if ( $content -match "FORCED SHUTDOWN" ) {
"Forced shutdown in Last Line"
}
else {
#Compares the current time to the Log date Time
#If Time difference is greater than 5 Minutes Send email.
if ((New-TimeSpan -end $logDateTime).minutes -lt -5) {
Send-MailMessage @splat
}
}