How can i get the output

Welcome Forums General PowerShell Q&A How can i get the output

This topic contains 7 replies, has 5 voices, and was last updated by

 
Participant
1 year, 4 months ago.

  • Author
    Posts
  • #77076

    Participant
    Points: 5
    Rank: Member

    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

  • #77079

    Participant
    Points: 34
    Rank: Member

    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

    • #77082

      Participant
      Points: 5
      Rank: Member

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

  • #77085

    Participant
    Points: 264
    Helping Hand
    Rank: Contributor

    ... and

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

    does not work??

    • #77139

      Participant
      Points: 5
      Rank: Member

      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
      
    • #77160

      Participant
      Points: 10
      Rank: Member
      $TestResults.TestResult | Select-Object -Property Name,
      Time,Result,@{n='Date';exp={get-date}}
      
    • #77188

      Participant
      Points: 5
      Rank: Member

      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

  • #77088
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    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.

The topic ‘How can i get the output’ is closed to new replies.