Link to home
Start Free TrialLog in
Avatar of Skumar_CCSA
Skumar_CCSAFlag for India

asked on

Powershell email alerts with html format

hi,

i have script working.
When it runs it check for files and send email alert if not files and also if has files.
Currently it sending the email alerts text format.
Request experts help, want to send email with htm format.
and for errors it should bold the text and color RED.
can help...


#Requires -version 3.0
$WarningPreference="Continue"
function Send-Mail
{
  param (
    [string] $Email_Subject,
    [string] $Email_Body,
    [string] $Email_Attachment
  )
  Send-MailMessage -SmtpServer 'mailserver.com' `
  -Body $Email_Body `
  -From 'donotreply@mail.com' `
  -To 'admin@mail.com' `
  -Subject $Email_Subject `
  -Attachments $Email_Attachment
}
function Send-Error
{
  Write-Host "Sending Error Alert @ $emaildate"
  $subject = "ACTION REQUIRED : Error Alert @ $emaildate"
  $logcontents = get-content $Logfile | Out-String
  $Body = "Dear All,`n`nThis email sent by system for errors highlighted below."  + $logcontents + "`n Regards,`n Job Process"
  Send-Mail -Email_Subject $subject -Email_Body $Body -Email_Attachment $Logfile }
function send-status
{
  Write-Host "Sending Success Alert @ $emaildate"
  $subject = "INFO : Success Alert @ $emaildate"
  $logcontents = get-content $Logfile | Out-String
  $Body = "Dear All,`n`nThis email sent by system for your information"  + $logcontents + "`n Regards,`n Job Process"
  Send-Mail -Email_Subject $subject -Email_Body $Body -Email_Attachment $Logfile }

  #check if source files exists
$sourcefolder="D:\ACES\SOURCE"
$backupFolder="D:\ACES\Archive"

$files   = 'CAT.xml', 'BAT.xml', 'RAT.xml'
$success = $true
foreach( $file in $files )
{
       $filename = Join-Path $sourcefolder $file
       if( Test-Path -LiteralPath $filename )
       {
              LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) FILECHECK:`tFile exists: $filename"
       }
       else
       {
              $msj = "The source file '$fileName' doesn't exist"
              LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ERROR:`t$msj"
                    LogWrite -ForegroundColor Red $msj
              $success = $false
       }
}

if( -not $success  )
{
    LogWrite "`n$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ACTION REQUIRED:`tFiles missings($sourcefolder)"
    send-Error
    exit -1
}
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is a duplicate of an open question: https://www.experts-exchange.com/questions/29119166/Powershell-script-errors-logs-to-send-email.html in which the -bodyashtml was already mentioned.
Avatar of Skumar_CCSA

ASKER

firstly it is not duplicate..the previous question posted for email alerts and issues in receiving email alerts.
This question is more on HTML format and additionally the errors to be bold and colored.