Discard missing log file and send email with available files

I wrote a code to perform data replication from one server to another, post successful replication, logs are generated at two path “\server1Files” and “\server1CAS”. the code is working fine and can send multiple log files as an attachment. The code executes once in a day, there can be a scenario where no new files are available for replication on source server, hence log file for missing files and copied files will not generate but only output log will generate. log file names are in Array.

Now I also want to check that even if any of the log file(s) is not generated or not available at mentioned location/path the remaining availbale log files must be attached and email should be sent.

I wrote below code to send all logs as an attachment in email whihc is working perfectly

   $User = "Email_DL";
   $Recipients = @("user1.xyz.com","user2.xyz.com");
   $Password = "";

   $Attachmentss = @(
                 "\Server1FilesFilesMissing.log"
                 "\Server1FilesFilesCopied.log"
                 "\Server1FilesOutput.log"
                 "\Server1FilesCASCAS_FLDFilesCopied.log"
                 "\Server1FilesCASCAS_FLDFilesMissing.log"
                 "\Server1FilesCASCAS_FLD_Output.log"
                );

   Send-MailMessage -From $User -To $Recipients -SMTPServer "XXXXXXXX" -Body " log files" -Subject `
   "Replication test" -Attachments $Attachmentss.

Now What I am looking for is even if any of the log file or files not available/generated even though the available log files in above path should be attached and emailed.
Example: if FilesMissing.log file is not available at path mentioned in above array the remaining files in array must be sent as an attachment, same way for remaining log files, but Output.log and CAS_FLD_Output.log will always generate post script completion. So, if no log files available then output logs should be attached and sent to Recipients.

Please let me know how to do it.

If I understand the question properly, the two files Output.log and CAS_FLD_Output.log wil always exist, even if none of the other files can be found.

In that case, I would suggest using a Where-Object clause to filter for only existing files in the predefined array.

Also, since I hate those nasty backticks in code, I wil use Splatting the parameters for commands that can take a lot of parameters.

$User       = 'Email_DL'
$Recipients = 'user1.xyz.com','user2.xyz.com'
$logFiles   = '\Server1FilesFilesMissing.log',
              '\Server1FilesFilesCopied.log',
              '\Server1FilesOutput.log',
              '\Server1FilesCASCAS_FLDFilesCopied.log',
              '\Server1FilesCASCAS_FLDFilesMissing.log',
              '\Server1FilesCASCAS_FLD_Output.log'

# test if the files exist. As I understand it, both
# Output.log and CAS_FLD_Output.log will Always exist, so
# $Attachments will at least always contain these two file paths
$Attachments = $logFiles | Where-Object { (Test-Path -Path $_) }

# use splatting to Send-MailMessage by creating a Hashtable with the parameters
$mailParams = @{
    From        = $User
    To          = $Recipients
    SMTPServer  = 'XXXXXXXX'
    Body        = 'log files'
    Subject     = 'Replication test'
    Attachments = $Attachments
    # add more parameters here when needed
}
# send the email
Send-MailMessage @mailParams

You may consider using parameter -LiteralPath instead of -Path on the Test-Path command to avoid errors when path and filenames could contain characters like [ or ]

4

This isn’t too bad, you just need to cycle through the list of files and verify they exist, and then add them to you array of attachments if they exist.

 $desiredAttachments = @(
                 "\Server1FilesFilesMissing.log"
                 "\Server1FilesFilesCopied.log"
                 "\Server1FilesOutput.log"
                 "\Server1FilesCASCAS_FLDFilesCopied.log"
                 "\Server1FilesCASCAS_FLDFilesMissing.log"
                 "\Server1FilesCASCAS_FLD_Output.log"
                );

$actualAttachments = @();
$missingFiles = @();

foreach($file in $desiredAttachments){
   $fileExists = Test-path $file -quiet

   if($fileExists){
      $actualAttachments += $file
   }
   else{
      Write-output "File $file was not present"
      $missingFiles += $file
   }
}

Then update your code to use $actualAttachments as the attachment and include $missingFiles to denote that those files were not present or weren’t found.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật