Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text log from an automated test that looks something like this:


Start: 2022-08-01 19:18:39 - Auto_Performance_Test

TestCase_Import SUCCESS 19.851s

TestCase_VerifySOMETHING_begin SUCCESS 1.339s

TestCase_VerifySOMETHING_complete SUCCESS 0.586s

TestCase_CheckForSOMETHING SUCCESS 0.416s

TestCase_Delete SUCCESS 2.131s

TestCase_Copy SUCCESS 0.510s



I would like to write a PowerShell script that will read the log file and obtain the execution times for each test case. The execution times will then need to go into the cells of an Excel spreadsheet column. The Excel file is a template test report where each test case is mapped to a corresponding program requirement.

So, the script needs read the log, one line at a time, and parse each line, excluding everything except the decimals, and store each as a string in an array. What is the best way to go about this? Would a regular expression work parsing the string? How would I code that in PowerShell?

What I have tried:

PowerShell
$mylog = Get-Content "C:\logfile.log"
$array = @()
$regex = "/^\d+\.?\d*$/"

$mylog | ForEach-Object
{
     if($_ -match $regex)
     {

     }
}
Posted
Updated 5-Aug-22 5:28am

1 solution

Here is a starter that can extract all the time values:
PowerShell
$regex = 'TestCase_'

foreach($line in Get-Content -Path $mylog) 
{
    if($line -match $regex)
    {
        $test, $success, $time = $line.Split()
        $time = $time.TrimEnd('s')
        Write-Output $time
    }
}

I have not used Excel from Powershell so am not sure what to do with the time fields. You could take a look at Introducing the PowerShell Excel Module - Scripting Blog[^], which is a very useful website.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900