Created
March 12, 2019 15:31
-
-
Save mrxinu/b672bc2197a4be1944a3298c447a8279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Installed as a PowerShell script monitor, this will look at a file and | |
# determine how many times a string has appeared since the last polling | |
# interval. | |
# | |
# The Scripts Arguments line should read: | |
# | |
# \\path\to\filename,"string to match" | |
# | |
# Functions | |
# Get-StringHash by Jon Gurgul (https://gallery.technet.microsoft.com/scriptcenter/Get-StringHash-aa843f71) | |
function Get-StringHash() { | |
Param( | |
[String] | |
$String | |
) | |
$StringBuilder = New-Object System.Text.StringBuilder | |
[System.Security.Cryptography.HashAlgorithm]::Create("MD5").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{ | |
[Void]$StringBuilder.Append($_.ToString("x2")) | |
} | |
$StringBuilder.ToString() | |
} | |
# Script | |
# $filePath = "\\path\to\some\file\here" | |
# $matchString = "some string here" | |
$filePath = $args.Get(0) | |
$matchString = $args.Get(1) | |
# to avoid overlap on the temp file name, create a hash made up of the | |
# path that's being monitored and the string that's being watched | |
$hash = Get-StringHash -String $filePath + $matchString | |
$tempFilePath = Join-Path -Path $env:TEMP -ChildPath $hash | |
if (Test-Path $tempFilePath) { | |
$seenRows = Get-Content $tempFilePath | |
} else { | |
$seenRows = 0 | |
} | |
# fail with a message if the file path is unavailable | |
if (-not (Test-Path $filePath)) { | |
Write-Output "Static: -1" | |
Write-Output "Message: Could not find file at path $($filePath)." | |
Exit 1 | |
} | |
# grab file and get linecount | |
$reader = [System.IO.File]::OpenText($filePath) | |
while($null -ne ($reader.ReadLine())) { | |
$lineCount ++ | |
} | |
# either we've never seen this file or it's been refreshed, start over | |
if ($seenRows -eq 0 -or $seenRows -gt $lineCount) { | |
# starting point will be line 1 | |
$seenRows = 0 | |
} elseif ($seenRows -eq $lineCount) { | |
$reader.Close() | |
Exit | |
} | |
# set the current line count in the temp file | |
Set-Content -Path $tempFilePath -Value $lineCount | |
# search the content for the string and produce a count | |
$counter = 0 | |
#$reader = [System.IO.File]::OpenText($filePath) | |
#$reader.DiscardBufferedData() | |
$pos = $reader.BaseStream.Seek(0, [System.IO.SeekOrigin]::Begin) | |
while($null -ne ($line = $reader.ReadLine())) { | |
$counter++ | |
if ($counter -gt $seenRows){ | |
$matchCount += ($line | Select-String "$matchString" | Measure-Object).Count | |
} | |
} | |
$reader.Close() | |
Write-Output "Statistic: $matchCount" | |
Exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment