Send Pester Output over email?

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. Active Directory Operations Test | pshirwin

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

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)

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             : {}

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

$resultHTML = $result.testResult | ConvertTo-Html | Out-String