Link to home
Start Free TrialLog in
Avatar of demichel
demichelFlag for United States of America

asked on

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.
Avatar of SubSun
SubSun
Flag of India image

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

Avatar of demichel

ASKER

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

Was posting, but then saw Subsun already had about the same code... :)
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
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect , the script work superbly! Thank you.
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