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