Send email when numeber is greater than

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Windows LiveUser151 Windows LiveUser151 7 months, 2 weeks ago.

  • Author
    Posts
  • #38046
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    I am trying to write a script that will look at a log file. There is a line in the file called TOTAL_TIME= . I would like to send an email if TOTAL_TIME is greater than 50000
    Below is a sample of what the file looks like.

    root-aborted=false
    innerScript=false
    START_TIME=1461098129524
    TOTAL_TIME=23406
    scriptSection=Creation
    root-storeVersion=1

  • #38047
    Profile photo of random commandline
    random commandline
    Participant

    What do you need in the email body? Is 'Total_time' on multiple lines in a log file? How many log files do you need to parse?

  • #38048
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    I would like the body to say that the search time is at "TOTAL_TIME"
    The Total time is only on one line in the text file, I only need to do this on one log file.

    Thanks

  • #38052
    Profile photo of Anthony Stringer
    Anthony Stringer
    Participant
    get-content c:\temp\logfile.log | ? {$_ -match 'total_time='} | % {
    	$totaltime = $_.split('=')[-1]
    	$somenumber = 50000
    	if ($totaltime -gt $somenumber) {
    		#send-mailmessage ...
    	}
    }
    
  • #38057
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    For some reason the logic will only work up to 9999 If I try 50000 it seem to work.. Is this limited to 4 digits? Here is my script. If I change the gt 50000 to 5000 it works. The value in the log file is TOTAL_TIME=7640

    Get-Content 'c:\test\overview.properties' |? {$_ -match 'total_time='} | % {
    $totaltime = $_.split('=')[-1]
    if ($totaltime -gt50000)
    {
    Send-MailMessage -From test-Subject "Searches are above 50000" -To test -Body "test" -SmtpServer xxx.xx.com
    }
    ELSE
    {
    Write-host "Not above 50000"

  • #38066
    Profile photo of random commandline
    random commandline
    Participant

    Cast your totaltime variable as an integer and your code should work.

    Get-Content '.\logfile' | ForEach-Object {
        [void]($_ -match 'TOTAL_TIME=.*')
        $time = ($Matches[0] -split '=')[-1]
        If ([int]$time -gt 9999){Send-MailMessage}
    }
    
  • #38070
    Profile photo of Windows LiveUser151
    Windows LiveUser151
    Participant

    Thanks. It works like a charm!!

You must be logged in to reply to this topic.