question

FoxsTail-8197 avatar image
0 Votes"
FoxsTail-8197 asked FoxsTail-8197 commented

Powershell - compare time in log file

Hello everyone, can you help me with this question? I need to compare two strings with time. I have a log file that contents information looks like that: 22.03.2021 18:59:16.111 | some information.... I need to get frome there only information about time and compare it with time is now. So I have a script:

 $MD = get-date - format "HH:MM:s"
      $path = some path to log file 
     $a = get-content -path $path -tail 1
      $b = $a.SubString(11,8)


Then i need to use if statements as I think:

 If (($MD).AddHours(-0,5) -gt $b) {do something} 
     else {do anotherthing}

But it will not works because $mb and $b is a string data :( how I can compare it?






windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
1 Vote"
IanXue-MSFT answered FoxsTail-8197 commented

Hi,

PowerShell converts $b into a datetime object automatically. Actaully it's (get-date).AddHours(-0,5) -gt [datetime]$b. You can also try

 ([datetime]$MD).AddHours(-0.5) -gt [datetime]$b

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the help and explanation. Will know it now.

0 Votes 0 ·
FoxsTail-8197 avatar image
0 Votes"
FoxsTail-8197 answered RichMatheisen-8856 commented

Miracle...
If change

 If ((get-date).AddHours(-0,5) -gt $b)

All is works... but still can't understand how it works if $b looks like a string data oO?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The variable "$b" does contain a string. But behind the scenes PowerShell is using that string to create a DateTime object so it can correctly compare it with the DateTime object created by your subtracting 1/2-hour from the variable "$MD".

The problem you may run into is that the log file doesn't contain a date, only a time. So in the implicit conversion of $b the time will use "today" as the date.

0 Votes 0 ·

No no. In here is doesn't work ($MD).AddHours(-0.5). I have a message that $MD is null or some other error. But time from a log file powershell use correctly. And in the log file i have a date. But I don't need to use it because it only one day log file. In the next day it will created as a new log file.
What is strange that $MD contains data as HH:MM:s but it filtred by the option -filter. Maybe this option breaks all. Anyway hugely thank you for the answer and help.

0 Votes 0 ·

Nobody said to use " ($MD).AddHours(-0.5) ". In that context, $MD contains a string and a string object has no "AddHours" method.

What was suggested was " ([datetime]$MD).AddHours(-0.5) ". In this context, the string in $MD is cast as a DateTime object, which does have the AddHours method.

0 Votes 0 ·
RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered FoxsTail-8197 commented

See if this makes more sense:

 $a = '1234567890112:10:139876'  # contains log file date data
 $MD = (Get-Date).AddHours(-0.5)
 $b = get-date $a.Substring(11,8)    # assumes that time is in 24 hour format (no AM/PM necessary)
    
 if ($MD -gt $b){
     "{0:HH:MM.s} > {1:HH:MM.s}" -f $MD, $b
 }
 else{
     "{0:HH:MM.s} <= {1:HH:MM.s}" -f $MD, $b
 }

It's better if you don't treat date/time as strings. That almost always leads to grief. Get the information into a DateTime object and your comparisons will make a lot more sense.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just wow. Thank you for explaining it. Will try to use it. Now I understand it little bit more for using it in the future.

0 Votes 0 ·