Skip to content

Instantly share code, notes, and snippets.

@ghotz
Last active October 7, 2022 05:13
Show Gist options
  • Save ghotz/f7b0fcf7abe0330ce41be9a1473ef4e2 to your computer and use it in GitHub Desktop.
Save ghotz/f7b0fcf7abe0330ce41be9a1473ef4e2 to your computer and use it in GitHub Desktop.
Filter Relog counter files by SQL Server database names
# extract counters first with relog.exe .\myfile.blg -q > counters.txt
$currentPath = $PSScriptRoot # AzureDevOps, Powershell
if (!$currentPath) { $currentPath = Split-Path $pseditor.GetEditorContext().CurrentFile.Path -ErrorAction SilentlyContinue } # VSCode
if (!$currentPath) { $currentPath = Split-Path $psISE.CurrentFile.FullPath -ErrorAction SilentlyContinue } # PsISE
if ($currentPath) { Set-Location $currentPath }
#$Databases = ("databasename");
$Databases = @(Get-Content (Join-Path $currentPath "databases.txt"));
$LinesRead = 0;
$CounterFileName = Join-Path $currentPath "counters.txt"
$FilteredFileName = $CounterFileName.Replace(".txt", "_filtered.txt");
try {
$SourceFile = New-Object -TypeName System.IO.StreamReader -ArgumentList $CounterFileName;
$FilteredFile = New-Object -TypeName System.IO.StreamWriter -ArgumentList $FilteredFileName;
while (-not $SourceFile.EndOfStream) {
$Line = $SourceFile.ReadLine().ToString();
if ($Line -match "\\\\.*\\.*\\.*") {
if ($Line -match "\\\\.*\\(?:SQLServer|MSSQL\$.*):Databases\((?<DatabaseName>.*)\)\\.*") {
if ($matches["DatabaseName"] -in $Databases) {
$FilteredFile.WriteLine($Line)
}
}
else {
$FilteredFile.WriteLine($Line)
}
}
$LinesRead++;
if ($LinesRead % 5000 -eq 0) { Write-Host "Reading line $LinesRead"; };
}
}
finally {
$SourceFile.close()
$FilteredFile.close()
}
# use the filtered counters file to filter the blg file e.g. relog .\MyFile.blg -cf .\counters_filtered.txt -o .\MyFile_filtered.blg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment