ETidalgo
7/30/2018 - 8:02 PM

Select-string - get context and other trip ups

function GetLogStatsForBusinessRelationshipScript {
    param([string]$FileName)

    function NewScriptStatistic{
        param(
            [string]$ExecutingBusinessResourceId,
            [string]$DateTimeNZLocal,
            [string]$DurationMicroSeconds,
            [string]$HasSoldTo
            )
        new-object psobject -property @{
            ExecutingBusinessResourceId = $ExecutingBusinessResourceId;
            DateTimeNZLocal = $DateTimeNZLocal;
            DurationMicroSeconds = $DurationMicroSeconds;
            HasSoldTo = $HasSoldTo;
        }
    }

    write-host $FileName
    sls -path $FileName -pattern " end IsCustomer" -Context 60,20 | % {
        $context = $_.Context
        $context.PreContext | ? { $_ -match ".*InformationSqlQuery.*" } | select -last 1 | % { $_ -match "(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d),[\d\-]*,InformationSqlQuery,Executing streaming SQL statement on connection [a-zA-Z0-9\-]* \(time to first row (\d+) microseconds\).*" } | out-null
        $dateTime = $Matches[1]
        $durationMicroseconds = $Matches[2]

        $context.PostContext | ? { $_ -match '"executingBusinessResourceId":(\d+)' } | select -first  1 | out-null
        $executingBusinessResourceId = $Matches[1]

        $matchSoldTo =  ( $context.PostContext | ? { $_ -match 'end IsSoldToParty' } )
        $hasSoldTo = ($matchSoldTo.Length -gt 0)

        NewScriptStatistic $executingBusinessResourceId $dateTime $durationMicroseconds $hasSoldTo
    }
}

$searchTerm = 'Only one usage of each socket address' 
sls -pattern $searchTerm -path *.log -list | % {
    $filename = $_.Filename
    "`n$filename"
    sls -path $filename -pattern '^Started ' -list | % { $_ -match "(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)" | out-null ; "$($Matches[1])"}

    sls -path $filename -pattern $searchTerm -Context 5,0 | % {
        $context = $_.Context
        $context.PreContext | ? { $_ -match "\.Given_" } | select -last 1 | % { $_ -match ".*(ECargo.*\.Given_[\w_]*).*" } | out-null
        $testName = $Matches[1]

        "`t$testName"
    }
} | add-content -encoding UTF8 .\Report.txt

## sls -Path *.log -pattern 'the database operation timed out' -Context  5,0 | % { Get-TestNameFromBuildLogContext $_ } 

function Get-TestNameFromBuildLogContext{
    param([Microsoft.PowerShell.Commands.MatchInfo][parameter(mandatory=$true,ValueFromPipeline=$True)]$MatchInfo)

    $context = $_.Context
    $context.PreContext | ? { $_ -match "\.Given_" } | select -last 1 | % { $_ -match ".*(ECargo.*\.Given_[\w_]*).*" } | out-null
    $testName = $Matches[1]

    "`t$testName"
}
# Groups[0] is the default group of general matching
# Groups[1] is the first captured group, Groups[2] is the next captured group, etc
gc "C:\Users\ernest.tidalgo\Documents\WindowsPowerShell\[script]" | select-string -Pattern "function ([\w-]+)" | select -ExpandProperty Matches | % { $_.Groups[1] } 
gc "C:\Users\ernest.tidalgo\Documents\WindowsPowerShell\[script]" | select-string -Pattern "function ([\w-]+)" | select -ExpandProperty Matches | % { $_.Groups[1].Values }