Author Posts

August 10, 2017 at 10:36 am

Hi,

I have an array which contains a load of test results.
I need to cycle from each result and output the value into a CSV file.

The array is created from the output of a pester test:

$TestResults.TestResult
ErrorRecord            : Expected: value was {Connection Failed}, but should not have been the same
ParameterizedSuiteName : 
Describe               : Network Test Status
Parameters             : {}
Passed                 : False
Show                   : All
FailureMessage         : Expected: value was {Connection Failed}, but should not have been the same
Time                   : 00:00:14.5828361
Name                   : Connect to Database
Result                 : Failed
Context                : 
StackTrace             : at , C:\ScriptOutPut\PesterTests\Network_Pester.Test.ps1: line 13
                         13:             $SQLResult | should not be "Connection Failed"

ErrorRecord            : 
ParameterizedSuiteName : 
Describe               : Network Test Status
Parameters             : {}
Passed                 : True
Show                   : All
FailureMessage         : 
Time                   : 00:00:03.0306725
Name                   : Test Connection to Remote Server
Result                 : Passed
Context                : 
StackTrace             : 

ErrorRecord            : 
ParameterizedSuiteName : 
Describe               : Network Test Status
Parameters             : {}
Passed                 : True
Show                   : All
FailureMessage         : 
Time                   : 00:00:00.6058733
Name                   : Copy File
Result                 : Passed
Context                : 
StackTrace             : 

I can run a foreach loop to filter the values i want but only if i use write-host, but obviously this wont export to CSV.

foreach ($Result in $TestResults.TestResult){
write-host $Result.Name, $Result.Time,$Result.Result
}

Output

Connect to Database 00:00:14.5828361 Failed
Test Connection to Remote Server 00:00:03.0306725 Passed
Copy File 00:00:00.6058733 Passed

Any ideas would be mush appreciated

Thanks

TommyQ

August 10, 2017 at 10:54 am

you might be able to use a hash table something like (untested)

$hash = @{}

Foreach ($results in $testResults.TestResult){
$hash.Add($results.Name,$results.Time,$results.Result)
}

$hash | export-csv -path C:\temp\mycsv.csv

August 10, 2017 at 11:02 am

Thanks Simon, I'd already tried that approach, but still no luck.

August 10, 2017 at 12:04 pm

... and

$TestResults.TestResult | Select-Object -Property Name,Time,Result

does not work??

August 10, 2017 at 12:58 pm

What does the whole script look like? A powershell script can only output one type of object at a time because of format-table implicitly running at the end.

August 10, 2017 at 3:42 pm

Hi Olaf, that worked! Knew it had to be easy.
Just one more thing, with that output is there anyway to add another column output.

this is what i have now:

Name                             Time             Result
----                             ----             ------
Connect to Database              00:00:14.5828361 Failed
Test Connection to Remote Server 00:00:03.0306725 Passed
Copy File                        00:00:00.6058733 Passed

Ideally id like to add:

Name                             Time             Result Date
----                             ----             ------ -----
Connect to Database              00:00:14.5828361 Failed 10/08/2017
Test Connection to Remote Server 00:00:03.0306725 Passed 10/08/2017
Copy File                        00:00:00.6058733 Passed 10/08/2017

August 10, 2017 at 6:57 pm

$TestResults.TestResult | Select-Object -Property Name,
Time,Result,@{n='Date';exp={get-date}}

August 11, 2017 at 8:26 am

Hi Random, thanks for the reply. That didn't work for me i had already tried, its almost there but just adds the date to the first row.

Anyway i seem to have found a way by using:

Add-Member -NotePropertyName Time -NotePropertyValue $Time
Name                           Time             Result Date 
----                           ----             ------ ---- 
Connect to Database            00:00:00.0350361 Passed 11:34
Copies within 700ms            00:00:00.6227508 Passed 11:34
Ping round trip less than 50ms 00:00:00.0520020 Passed 11:34
Disk Queue length less than 2  00:00:00.9977253 Passed 11:34

Cheers

TommyQ