Hi,
I have two log files (printsmith.log, printsmith.log.1). The below script is to monitor if aggregation job is completed sucessfully or not.
Script is working fine without any issue (it gives me the output in the console). I am facing issue, writing the output to a file and send the email.
> Action1 must send an email, if the Aggregation did not start for the date.
> Action2 must send an email, if Aggregation did not complete yet for the date.
> Action3 must send an email, if Aggregation completed successfully.
I am facing issues looping the e-mail part. Can someone help me on this. I marked bold in the script, where I am facing issues.
#declare variables $logsourcepath = 'C:\Program Files (x86)\EFI\PrintSmith\PrintSmith Vision Server\logs\' $logpath = 'C:\opscripts\Kalyan\Vision_Data_Aggregation_Daily_Check\log\' $logfile1 = 'printsmith.log' $logfile2 = 'printsmith.log.1' $logfile1path = $logpath + $logfile1 $logfile2path = $logpath + $logfile2 $logfile1sourcepath = $logsourcepath + $logfile1 $logfile2sourcepath = $logsourcepath + $logfile2 $previous_date = (Get-Date).AddDays(-1) $agg_start_date = (Get-Date $previous_date -Format 'yyyy-MM-dd') $agg_end_date = (Get-Date -Format 'yyyy-MM-dd') #copy latest log files and delete existing ones if ((Test-Path $logfile1path) -and (Test-Path $logfile2path)) { rm -Force $logfile1path rm -Force $logfile2path } cp $logfile1sourcepath $logpath cp $logfile2sourcepath $logpath ### MAIN FUNCTION ### #search printsmith.log to find start of aggregation cd C:\opscripts\Kalyan\Vision_Data_Aggregation_Daily_Check #find if aggregation started or not $agg_start = (.\sed.exe -n -e "/^ERROR $agg_start_date 22:30:00/p" $logfile1path) if ($agg_start -lt 1) { $agg_start = (.\sed.exe -n -e "/^ERROR $agg_start_date 22:30:00/p" $logfile2path) if ($agg_start -lt 1) { Write-Host -ForegroundColor Red "ERROR: Server01 Aggregation did not start for the date: $agg_start_date" } elseif ($agg_start | Select-String "Data Aggregation Trigger start") { Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation started successfully for the date: $agg_start_date which is recorded in the log file: $logfile2" Write-Host "EXTRACT FROM LOG FILE:" $agg_start[0] } else { Write-Host -ForegroundColor Red "ERROR: Server01 Aggregation did not start for the date: $agg_start_date" } } elseif ($agg_start | Select-String "Data Aggregation Trigger start") { Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation started successfully for the date: $agg_start_date which is recorded in the log file: $logfile1" Write-Host "EXTRACT FROM LOG FILE:" $agg_start[0] } else { Write-Host -ForegroundColor Red "ERROR: Server01 Aggregation did not start for the date: $agg_start_date" } #send mail and terminate script if aggregation did not start #find if aggregation completed if ($agg_start | Select-String "Data Aggregation Trigger start") { $agg_end = (./sed.exe -n -e "/End Time - $agg_end_date/p" $logfile1path) if ($agg_end -lt 1) { $agg_end = (./sed.exe -n -e "/End Time - $agg_end_date/p" $logfile2path) if ($agg_end -lt 1) { Write-Host -ForegroundColor Yellow "WARNING: Server01 Aggregation did not complete yet for the date: $agg_start_date. Kindly investigate!!!" } else { Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation completed successfully and is recorded in the log file: $logfile2" $logline = $agg_end[0] Write-Host "EXTRACT FROM LOG FILE:" $logline Write-host "`n FINAL OUTPUT:" $final_output = (gc $logfile2path | Select-String $logline -Context 2,5 | Select-Object -First 1) Write-Host $final_output } } else { Write-Host -ForegroundColor Green "SUCCESS: Server01 Aggregation completed successfully and is recorded in the log file: $logfile1" $logline = $agg_end[0] Write-Host "EXTRACT FROM LOG FILE:" $logline Write-host "`n FINAL OUTPUT:" $final_output = (gc $logfile1path | Select-String $logline -Context 2,5 | Select-Object -First 1) Write-Host $final_output } } #send final mail based on result from above
-Kalyan