demichel
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.WebServiceM anager.DoU pdateSelec ted() : 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.
Output command:
PS C:\Users\ch> Get-Content C:\Users\ch\Documents\test
lect-Object -last 1
20140801.15:14:14 UTC : QBWebConnector.WebServiceM
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.
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\tes t.txt'.
At C:\Users\ch\Documents\aler t.ps1:1 char:9
+ $line = Get-Content Get-Content C:\Users\ch\Documents\test .txt | select-string " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun d,Microsof t.PowerShe ll.Command s.GetConte ntCommand
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\ch\Documents\aler t.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\aler t.ps1:3 char:5
+ If ((((Get-Date).ToUniversalT ime()) - $date).Hours -ge 3){Send-MailMessage -From ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBes t
PS C:\Users\ch\Documents> .\alert.ps1
Get-Content : A positional parameter cannot be found that accepts argument 'C:\Users\ch\Documents\tes
At C:\Users\ch\Documents\aler
+ $line = Get-Content Get-Content C:\Users\ch\Documents\test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\ch\Documents\aler
+ $date = [datetime]::ParseExact("$(
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
Cannot find an overload for "op_Subtraction" and the argument count: "2".
At C:\Users\ch\Documents\aler
+ If ((((Get-Date).ToUniversalT
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBes
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
Was posting, but then saw Subsun already had about the same code... :)
ASKER
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\aler t.ps1:3 char:59
+ If ((((Get-Date).ToUniversalT ime()) - $date).Hours -ge 3){Send-MailMessage -From ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClien t:SmtpClie nt) [Send-MailMessage], SmtpFailedRecipientExcepti on
+ FullyQualifiedErrorId : SmtpException,Microsoft.Po werShell.C ommands.Se ndMailMess age
Send-MailMessage : Mailbox unavailable. The server response was: must be authenticated
At C:\Users\ch\Documents\aler
+ If ((((Get-Date).ToUniversalT
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClien
+ FullyQualifiedErrorId : SmtpException,Microsoft.Po
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect , the script work superbly! Thank you.
ASKER
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,
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
Ref : http://ss64.com/ps/syntax-compare.html
Open in new window