PoweShell Get-Content Script to read a text file comparing data and time

I have this powershell command that read a text file for Quickbook Web Connector log and search for a specific text "No data to exchange for the application"  :

Output command:

PS C:\Users\ch> Get-Content C:\Users\ch\Documents\test.txt | select-string "No data to exchange for the application"| Se
lect-Object -last 1

20140801.15:14:14 UTC    : QBWebConnector.WebServiceManager.DoUpdateSelected() : No data to exchange for the
application:  www.abc.com - QB Financial Web Connector
I need to create a script that run every 3 Hours and compare the date and time if the last result was 3 Hours from the current time and send an email like an alert.

For Example:

The script need to check for the last result " See output above"  date and time "20140801.15:14:14 UTC....."  with the current time (when the script run) 20140801.18:14:14 and if it's more than > 3 Hours send an email with the results with the output .

Let me know if this is possible.

Thank you.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this..
$line = Get-Content Get-Content C:\Users\ch\Documents\test.txt | select-string "No data to exchange for the application"| Select-Object -last 1

$date = [datetime]::ParseExact("$(($line -split "UTC")[0].Trim())", "yyyyMMdd.HH:mm:ss", $null)

If ((((Get-Date).ToUniversalTime()) - $date).Hours -ge 3){
Send-MailMessage -From From.domain.com -To To@domain.com -Subject $line -SmtpServer smtp.domain.com

Open in new window

demichelAuthor Commented:
I'm getting this error :

PS C:\Users\ch\Documents> .\alert.ps1
Get-Content : A positional parameter cannot be found that accepts argument 'C:\Users\ch\Documents\test.txt'.
At C:\Users\ch\Documents\alert.ps1:1 char:9
+ $line = Get-Content Get-Content C:\Users\ch\Documents\test.txt | select-string " ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\ch\Documents\alert.ps1:2 char:35
+ $date = [datetime]::ParseExact("$(($line -split "UTC")[0].Trim())", "yyyyMMdd.HH ...
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
Cannot find an overload for "op_Subtraction" and the argument count: "2".
At C:\Users\ch\Documents\alert.ps1:3 char:5
+ If ((((Get-Date).ToUniversalTime()) - $date).Hours -ge 3){Send-MailMessage -From ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
There is a typo in first line..
$line = Get-Content C:\Users\ch\Documents\test.txt | select-string "No data to exchange for the application"| Select-Object -last 1

Open in new window

ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Was posting, but then saw Subsun already had about the same code... :)
demichelAuthor Commented:
Now I'm getting smtp authentication error can you send me the complete code to send email from smtp server with authentication ?

Send-MailMessage : Mailbox unavailable. The server response was: must be authenticated
At C:\Users\ch\Documents\alert.ps1:3 char:59
+ If ((((Get-Date).ToUniversalTime()) - $date).Hours -ge 3){Send-MailMessage -From ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpFailedRecipientException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Try using Credential  parameter with Send-MailMessage
$Pwd = ConvertTo-SecureString "YourPassword" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ("Yourusername", $Pwd)

Send-MailMessage -From From.domain.com -To To@domain.com -Subject $line -SmtpServer smtp.domain.com -Credential $Cred

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
demichelAuthor Commented:
Perfect , the script work superbly! Thank you.
demichelAuthor Commented:
It is possible to change to check only for the last 3 hours ? so the range of 3 hours time frame < 3 ? and include after send the email to run a batch file ?

Let me know

thank you,
In the code -ge 3 means... if the date difference is greater than or equal to 3. You can change this Comparison Operator to  -le 3 (Less than or equal).

Ref : http://ss64.com/ps/syntax-compare.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.