Sending Email based on a File Size?

First off let me state that I’m using Orchestrator to send the email through powershell. Unfortunately due to this constraint I can only write in Powershell v.1 code. I could call to an external powershell to help this matter but due to the other moving pieces I’m trying to keep it all in one place as much as possible. Think the below can be done?

I’m wanting to send a .htm file as html in the body of the e-mail. This .htm file is written to every day and sometimes contains no new data. I don’t want the email to send if there’s no added data so I’m measuring the size of the file. This is working however now if it’s lower than specified file size it just sends a blank email. I rather it send no email if the file size requirement is not met.

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = ""
$emailMessage.To.Add( "" )
$emailMessage.Subject = ""
$emailMessage.IsBodyHTML = $true
$emailMessage.Body = Get-Content ("$file") | Where-Object {$_.Length -gt 4KB}
$SMTPClient = New-Object Net.Mail.SmtpClient($emailSmtpServer, $emailSmtpServerPort)
$SMTPClient.EnableSsl = $true
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

Hope it can be done and appreciate any feedback!


Just a thought, why not put your send mail in a function. you could then test to see if the file is the correct size and then call the function and if it is not the correct size dont call the function.

Like this maybe?

If (Get-ChildItem c:\123.htm | where { $_.Length -gt 3kb}
Else {

I got it to work with that! Awesome!