How to troubleshoot body of email is blank in Powershell emailing?

CharlieLor 551 Reputation points
2023-03-27T19:11:11.0733333+00:00

This is my code. The email comes through but the body is blank. If I do sendMail("This is a test") it does come through with that text in the body though.

$servers = @("Server1","Server2","Server1","Server3")


##### Function to send mail #####
function sendMail([string]$msgbody) {
    $smtpServer = "SMTP.myDomain.com" 
    $mailer = new-object Net.Mail.SMTPclient($smtpserver)
    $From = "noreply@myDomain.com"
    $To = "myName@myDomain.com" 
    $subject = "SSL Certificate Expiring Soon" 
    $msg = new-object Net.Mail.MailMessage($from,$to,$subject,$msgbody)   
    $msg.IsBodyHTML = $true 
    $mailer.send($msg)
}


##### Loop through all servers in the server array #####
foreach($server in $servers) {

    try{
    
        $expiredCert += Invoke-Command -ComputerName $server -ScriptBlock {Get-ChildItem -Path Cert:\localmachine\my | ?{$_.NotAfter -lt (get-date).AddDays(30)} | Select Thumbprint,FriendlyName,NotAfter, NotBefore}

    } catch {
        $ErrorMessage = $_.Exception.Message
        $currentTime = Get-Date
        sendMail("There was a problem connecting to and/or querying "+ $server +" for the failed Server login attempts. Specific error:`n`n" + $ErrorMessage + "`n`n Timestamp: " + $currentTime + "")
    }

}

if ($expiredCert){
    sendMail($expiredCert)
    Write-Output $expiredCert
}

The Write-Output $expiredCert shows the following in Windows PowerShell IISE.

User's image

The issue is sendMail of that variable just not coming through to my email.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,107 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2023-03-27T21:40:54.5833333+00:00

    Is $expiredCert an array or does it hold a single object? If it's a single object, it appears to be either a PSObject or a certificate.

    In your sendMail function you cast the value passed to the parameter $msgbody as a string. If you try to do this: $expiredCert.ToString() the results probably don't look like what you expect.

    I think what you want is to pass the results of this: $expiredCert | Out-String to the sendMail function.

    You might also try using the Send-MailMessage cmdlet instead of the Net.Mail.SMTPclient.

    function sendMail([string]$msgbody) {
        $props = @{
            Server = "SMTP.myDomain.com"
            From = "noreply@myDomain.com"
            To = "myName@myDomain.com"
            Subject = "SSL Certificate Expiring Soon" 
            Body = $msgbody
            BodyAsHTML = $true
        }
        # Note: assumes port 25 is used,
        #       the connection is anonymous, and
        #       TLS (SSL) is not used.
        Send-MailMessage @props
    }
    

  2. Paolo Miotti 0 Reputation points
    2023-03-27T21:47:21.3333333+00:00

    Hi @CharlieLor ,

    can you consider send the mail without your function but using that powershell comand :

    $body="There was a problem connecting to and/or querying "+ $server +" for the failed Server login attempts. Specific error:`n`n" + $ErrorMessage + "`n`n Timestamp: " + $currentTime + ""
    Send-MailMessage -To “myName@myDomain.com” -From “noreply@myDomain.com”  -Subject “SSL Certificate Expiring Soon” -Body $body -SmtpServer “SMTP.myDomain.com” -Port 25 
    
    

    Hope I was Help you

    My regards

    Paolo