How to get numbers in the end of my string. PowerShell

Viewed 145

I have a big txt file with repeating strings. Some strings have numbers in the end, so, I need to output only number to a variable for further use in Write-Host. These numbers can change because the file is a test case report and there is no key = value format inside the file. how to did it? This is the script I am using

$file = Get-ChildItem -Path $reportDir -Include $reportName'.'$fileformat -Recurse | sort-Object LastAccessTime 
#$A = Get-Content ($reportDir + $SourceFile) 
$report=Get-Content ($file) | Select-String -Pattern "Number of test steps" 
$report= ???

write-host "{
`"operation`": `"$environment`",
`"status`": `"OK`",
`"ProjectName`": `"$project`",
`"TestSuit`": `"$testSuite`",   
`"TestCase`": `"$testCase`",
`"TestStep`": `"$value`",
`"FailStep`": `"$value`",
`"Coverage`": `"$value`",
}"

Now my output looks this:

         Number of test steps                      16     
                                    
{
"msg": "RESULT",
"operation": "Default environment",
"status": "OK",
"ProjectName": "$project",
"TestSuit": "Account",   
"TestCase": "AccountCases",
"TestStep": "...",
"FailStep": "AccountCases",
"Coverage": "AccountCases",
}

This is part of my report file, there are lines of numbers from which I need Overview
Project M2M web API Box

     Test suite name                           Account                                         
     Test case name                            AccountCases                                    
                                                                                               
     Description                                                                               
                                                                                               
     Environment                               Default environment                             
                                                                                               
 Base Metrics                                                                                  
     Number of test steps                      16                                              
     Number of load tests                      0                                               
                                                                                               
     Number of assertions                      45  
    Start Time                                 Thu Dec 02 17:33:58 MSK 2021                    
                                                                                               
    End Time                                   Thu Dec 02 17:34:07 MSK 2021                    
    Time Taken                                 9171 ms                                         
                                                                                               
    TestStep Count                             16                                              
    Failed TestStep Count                      0                                               
                                                                                               
    Assertion Count                            45                                              
                                                                                               
    Failed Assertion Count                     0                                               
 Test Step Results                                                                             

I need 2 values from a parameter 'Number of test steps' and 'Failed TestStep Count'

1 Answers

You can parse this input file and generate a hashtable out of it which can be easily manipulated. Even though this works, the regex on the -split is likely to be improved but I'm no expert on that.

  • Assuming this file name is test.txt:
$hash = [ordered]@{}

Get-Content ./test.txt | ForEach-Object {
    if(-not [string]::IsNullOrWhiteSpace($_))
    {
        $key, $val = ($_ -split '\s{2,}' -ne '').Trim()
        $hash.Add($key, $val)
    }
}

This generates the following hashtable which you can easily reference:

Name                           Value
----                           -----
Test suite name                Account
Test case name                 AccountCases
Description                    
Environment                    Default environment
Base Metrics                   
Number of test steps           16
Number of load tests           0
Number of assertions           45
Start Time                     Thu Dec 02 17:33:58 MSK 2021
End Time                       Thu Dec 02 17:34:07 MSK 2021
Time Taken                     9171 ms
TestStep Count                 16
Failed TestStep Count          0
Assertion Count                45
Failed Assertion Count         0
Test Step Results              

For example (I would avoid Write-Host here):

# Not sure which key goes with each value here but as an example
[pscustomobject]@{
    operation   = 'Operation?'
    status      = 'OK'
    ProjectName = 'Project Name?'
    TestSuit    = $hash['Test suite name']
    TestCase    = $hash['Test case name']
    TestStep    = $hash['Number of test steps']
    FailStep    = $hash['Failed TestStep Count']
    Coverage    = 'Coverage?'
} | ConvertTo-Json

Sample returned JSON:

{
  "operation": "Operation?",
  "status": "OK",
  "ProjectName": "Project Name?",
  "TestSuit": "Account",
  "TestCase": "AccountCases",
  "TestStep": "16",
  "FailStep": "0",
  "Coverage": "Coverage?"
}
Related