Sending Email based on a File Size?

This topic contains 3 replies, has 2 voices, and was last updated by  Simon Says 2 weeks, 2 days ago.

  • Author
    Posts
  • #83356

    Simon Says
    Participant

    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 }
    $SMTPClient.Send($emailMessage)
    

    Hope it can be done and appreciate any feedback!

    Thanks!

  • #83369

    Simon B
    Participant

    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.

    • #83408

      Simon Says
      Participant

      Like this maybe?

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

    • #83431

      Simon Says
      Participant

      I got it to work with that! Awesome!

You must be logged in to reply to this topic.