The scenario is like you need to read the recent most file created from a directory, your script need to identify the most recent created file and then read it to get the count of logs generated in last one hour.
############################################################################# ## ## Updating Logs Count ## Created by Ataul Haque ## Date : 23 Sep 2016 ## Version : 1.0 ## Email: ataulhaque@outlook.com ############################################################################## $hostname = $env:computername $dir = "C:\ataul\" $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1 $latest.name $BOCLogFile = "$latest" ( Get-Content $BOCLogFile ) | Where { $_ } | Set-Content $BOCLogFile $count = 0 $reader = New-Object IO.StreamReader $BOCLogFile while($reader.ReadLine() -ne $null){ $count++ } $reader.Close() $LastLine = Get-Content $BOCLogFile | Select-Object -last 1 $items = $LastLine -split '\s+' $LastLogHour = $items[2].split(':') [int]$intNum = [convert]::ToInt32($LastLogHour[0], 10) $lastHour = [int]$intNum - 1; $lastHourLogLine = [string]$lastHour+":"+$LastLogHour[1]+":"+$LastLogHour[2] echo $lastHourLogLine $LineNumber = Select-String -Path $BOCLogFile -Pattern $lastHourLogLine | Select-Object -First 1 | Select-Object -ExpandProperty 'LineNumber' [int]$LinesAdded = [int]$count - [int]$LineNumber #echo $LinesAdded [string]$Outputreport = "Logs generated in Last One Hour: $LinesAdded"; echo $Outputreport #Now send this data in any way you like either using web services or using sendMail option
No comments:
Post a Comment