Send Pester Output over email?

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

Viewing 4 reply threads
  • Author
    Posts
    • #40629
      Participant
      Topics: 15
      Replies: 41
      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. https://pshirwin.wordpress.com/2016/04/08/active-directory-operations-test/

      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?

      Thanks,

      Michael

    • #40686
      Participant
      Topics: 51
      Replies: 86
      Points: 12
      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.To.Add("Guy@powershell.org")
      					$msg.Subject = "Subject here"
      					$msg.Body = "$($output)"
      					$smtp.Send($msg)
      

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

      $att = new-object Net.Mail.Attachment("c:\filename.txt")
      $msg.attachments.add($att)
      
    • #40741
      Participant
      Topics: 2
      Replies: 8
      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…

      $Results.TestResult

      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
      Participant
      Topics: 15
      Replies: 41
      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 "$env:COMPUTERNAME@domain.com" -Subject 'AD Operations Validation Report' -body $resultHTML -BodyAsHtml -To 'mmaher@domain.com' -SmtpServer 'hub.domain.com' 
      

      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 'mmaher@domain.com' -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       
      

      Thanks,

      Michael

    • #41024
      Participant
      Topics: 2
      Replies: 8
      Points: 0
      Rank: Member
      $resultHTML = $result.testResult | ConvertTo-Html | Out-String
      
Viewing 4 reply threads
  • The topic ‘Send Pester Output over email?’ is closed to new replies.