Send Pester Output over email?

Welcome Forums General PowerShell Q&A Send Pester Output over email?

This topic contains 4 replies, has 3 voices, and was last updated by

2 years, 7 months ago.

  • Author
  • #40629

    Points: 21
    Rank: Member

    Hi Folks,

    I have set up Irwin Strachan's AD Operations Validation tests in my organization. This utilities Pester to compare current Domain/Forest settings with a previously taken baseline.

    I'd love to set this up and have it send me an email every day. I'm only getting started with Pester. From what I have picked up this is useful but unconventional use of Pester. I'm wondering what is the best way to programatically take Pester output and send via an email. All I can think of is using Start-Transcript.

    Any ideas?



  • #40686

    Points: 0
    Rank: Member

    Not familar with pester, but you can always use the

                                            $output = "pester output"
    					$smtpServer = "SMTP server or IP"
    					$msg = new-object Net.Mail.MailMessage
    					$smtp = new-object Net.Mail.SmtpClient($smtpServer)
    					$msg.From = "pester@pester.mail"
    					$msg.Subject = "Subject here"
    					$msg.Body = "$($output)"

    If the output is saved as an file you can attach it using:

    $att = new-object Net.Mail.Attachment("c:\filename.txt")
  • #40741

    Points: 0
    Rank: Member

    Use Invoke-Pester with the -PassThru parameter. If you put this into a variable you could send the output to HTML and use that as the body of your email.

    $Results = Invoke-Pester .\sometests.tests.ps1 -PassThru

    Your content will reside in...


    Example output...

    Describe               : Active Directory configuration operational readiness
    Context                : Verifying Forest Configuration
    Name                   : Forest FQDN
    Result                 : Failed
    Passed                 : False
    Time                   : 00:00:00.4181933
    FailureMessage         : Expected: {aa}
                             But was:  {en-GB}
    StackTrace             : at line: 6 in C:\Users\andre\Desktop\it.tests.ps1
                             6:             Should be 'aa'
    ErrorRecord            : Expected: {aa}
                             But was:  {en-GB}
    ParameterizedSuiteName :
    Parameters             : {}
  • #40959

    Points: 21
    Rank: Member

    Thanks Folks,

    Almost there but hitting a funny issue. Here's my code

    $result = Invoke-Pester .\ADOperationalValidation.ps1 -PassThru
    $resultHTML = $result.testResult | ConvertTo-Html
    Send-MailMessage -From "$" -Subject 'AD Operations Validation Report' -body $resultHTML -BodyAsHtml -To '' -SmtpServer '' 

    But I'm getting this error on sending the report.

    Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
    At C:\Scripts\reports\mailReport.ps1:3 char:109
    + ...  Report' -body $resultHTML -BodyAsHtml -To '' -SmtpServer  ...
    +                    ~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

    But as you can see $resultHTML is a string so I am confused why it's being seen as an object?

    PS C:\Scripts\reports> $resultHTML | gm
       TypeName: System.String       



  • #41024

    Points: 0
    Rank: Member
    $resultHTML = $result.testResult | ConvertTo-Html | Out-String

The topic ‘Send Pester Output over email?’ is closed to new replies.