Send Pester Output over email?

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Andrew Palmer Andrew Palmer 6 months, 1 week ago.

  • Author
    Posts
  • #40629
    Profile photo of Michael Maher
    Michael Maher
    Participant

    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
    Profile photo of ertuu85
    ertuu85
    Participant

    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
    Profile photo of Andrew Palmer
    Andrew Palmer
    Participant

    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             : {}
    • This reply was modified 6 months, 2 weeks ago by Profile photo of Andrew Palmer Andrew Palmer.
  • #40959
    Profile photo of Michael Maher
    Michael Maher
    Participant

    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

    • This reply was modified 6 months, 1 week ago by Profile photo of Michael Maher Michael Maher. Reason: typo
  • #41024
    Profile photo of Andrew Palmer
    Andrew Palmer
    Participant
    $resultHTML = $result.testResult | ConvertTo-Html | Out-String
    

You must be logged in to reply to this topic.