0

I have the output of an Invoke-sqlcmd in a text file.

I need to check the output for the word failed or failure and if it's found, I need to break

here is my code:

$var=Get-Content "C:\JenkinsDrops\TRGDB0632 tSQLt Report.txt"
$count = 0
foreach($item in $var -match "Failure"){
    $count=$count+1
    out-host $item.ToString()
}

the output looks like this:

    [PayrollUnitTests].[test sp_GetEmployeePaySlipPayElements_Basic] failed: (Failure) Unexpected/missing resultset rows!
    |_m_|Descn                     |Units|Rate |Amount|Category |isGross|
    +---+--------------------------+-----+-----+------+---------+-------+
    |<  |Tronc Payment Caffe       |0    |77.96|77.96 |Payment  |False  |
    |=  |Back Of House Tronc Caffe |1    |27.29|27.29 |Deduction|False  |
    |=  |Champneys Springs Salaried|0    |0    |0.89  |Deduction|False  |
    |=  |Hour Caffe                |31   |7.2  |223.2 |Payment  |False  |
    |=  |NI                        |0    |0    |8.18  |Deduction|False  |
    |=  |Tax                       |0    |0    |12.4  |Deduction|False  |
    |>  |Tronc Payment Caffe       |1    |77.96|77.96 |Payment  |False  |

    +----------------------+
    |Test Execution Summary|
    +----------------------+

    |No|Test Case Name                                                  |Dur(ms)|Result |
    +--+----------------------------------------------------------------+-------+-------+
    |1 |[PayrollUnitTests].[test fn_GetEmployeeBradfordFactor]          |    413|Success|
    |2 |[PayrollUnitTests].[test sp_GetEmployeeDetailsforPreview]       |    220|Success|
    |3 |[PayrollUnitTests].[test sp_GetEmployeesDetailsForCalc]         |    520|Success|
    |4 |[PayrollUnitTests].[test sp_GetP11DetailsForEmployee]           |    290|Success|
    |5 |[PayrollUnitTests].[test sp_UpdateEmployeeP11FromCalc]          |    306|Success|
    |6 |[PayrollUnitTests].[test sprc_BACS_UpdateBACSStatus]            |    130|Success|
    |7 |[PayrollUnitTests].[test sp_GetEmployeePaySlipPayElements_Basic]|    333|Failure|

any occurrence of the word "fail" or "Failure" means the tSQLt unit tests have failed and the deployment to QA cannot proceed

what's the best way to do that?

2 Answers 2

2

This approach is a little simpler, but appears to do the trick. Does this work for what you are trying to do?

$failures = Select-String -Path "C:\JenkinsDrops\TRGDB0632 tSQLt Report.txt" -Pattern "Fail(ed|ure)?"

if ( $failures )
{
  $failures
  throw ("The unit tests were not successful!")
}

Basically, Search-String reads the entire contents of the file and matches any lines with the words "failed" or "Failure" as you specified. The matching lines are output and the script raises an exception.

Sign up to request clarification or add additional context in comments.

Comments

1

This is easier than you think.

Get-Content "C:\JenkinsDrops\TRGDB0632 tSQLt Report.txt" | % { if ($_ -match 'Failure' -or $_ -match 'Failed') {write-host $_} }

That will output the lines of your logfile that show the keyword matches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.