none
Where-Object and Get-Date problems (i think) RRS feed

  • Question

  • I am working on a log file and need to manipulate it to put the information into our graphing software (graphite).  Right now, the log file is pipe deliminated with the first column being the time and last column being the output.  I am able to get a count of the output that i need from the entire log file, but since i only want the output from the last 10 minutes, I am having a heck of a time manipulating the variables.

    Here is where i currently sit:

    ### Get the current time - 10 minutes
    $Last10Min = (Get-Date).AddMinutes(-10)
    
    ### Import working file
    $Data = Import-CSV -Delimiter '|' -Path "C:\LogFile.log" -Header LogDate,prop2,prop3,prop4,prop5,LogOutput;
    
    ### Change Column 1 Variable to DateTime (Already in UTC Format)
    $data|foreach-object{
        $_.LogDate = $_.LogDate -as [datetime]
        $_
    }
    
    ### Search the log for text AND lines only 10 minutes old
    $Last10MinutesofLogs = $data|Where-Object {$_.LogOutput -match "authenticated = true" -AND $_.LogDate -lt $Last10Min.ToUniversalTime()}
    
    ### Count the number of lines
    $SuccessfulLoginMetric = $Last10MinutesofLogs.Count
    
    ### Output the number of lines
    Write-Host $SuccessfulLoginMetric

    Any Suggestions to clean up the code would be appreciated.

    Thanks


    • Edited by Drabbies Thursday, December 10, 2015 6:43 AM corrected Title
    Thursday, December 10, 2015 6:42 AM

Answers

  • Too much unnecessary code:

    $lastten=Import-CSV -Delimiter '|' -Path "C:\LogFile.log" -Header LogDate, prop2, prop3, prop4, prop5, LogOutput |
    		Where-object{
    			[DateTime]($_.LogDate) -gt [datetime]::Now.AddMinutes(-10)
    		}
    $lastten.Count

    Assuming the log file it really what you say it is.


    \_(ツ)_/

    • Marked as answer by Drabbies Thursday, December 10, 2015 3:45 PM
    Thursday, December 10, 2015 6:57 AM

All replies

  • Too much unnecessary code:

    $lastten=Import-CSV -Delimiter '|' -Path "C:\LogFile.log" -Header LogDate, prop2, prop3, prop4, prop5, LogOutput |
    		Where-object{
    			[DateTime]($_.LogDate) -gt [datetime]::Now.AddMinutes(-10)
    		}
    $lastten.Count

    Assuming the log file it really what you say it is.


    \_(ツ)_/

    • Marked as answer by Drabbies Thursday, December 10, 2015 3:45 PM
    Thursday, December 10, 2015 6:57 AM
  • unnecessary code is my middle name.  Thank you very much for cleaning it up for me.  That is amazing.
    Thursday, December 10, 2015 3:45 PM
  • Always start with the least.  if you need to generalize or comment do ii after you have proven the method.

    Good luck.


    \_(ツ)_/

    Friday, December 11, 2015 12:03 AM