Search code examples
powershell

Send email based on log file generated/found at path


I wrote a code to replicate a path from one server to another and sending email with logs attached in it. The code is working perfectly. the below log files are generated post data replication is completed and if prod and cob servers already in sync and no new files found to copy to Server1 then only output file will generate.

             "\\Server1\Files\FilesMissing.log"
             "\\Server1\Files\FilesCopied.log"
             "\\Server1\Files\Output.log"
             "\\Server1\Files\CAS\CAS_FLDFilesCopied.log"
             "\\Server1\Files\CAS\CAS_FLDFilesMissing.log"
             "\\Server1\Files\CAS\CAS_FLD_Output.log"

But now I need few more checks based on scenario as below:

  1. In first elseif is if FilesMissing.log and CAS_FLDFilesMissing.log both are missing/not found then email message should go as " No new files to sync from source to destination"
  2. in Second elseif, if FilesMissing.log is missing but CAS_FLDFilesMissing.log and CAS_FLDFilesCopied.log are found then email message should go as "Files copied to CAS folder.
  3. In third elseif, if CAS_FLDFilesMissing.log is missing but FilesCopied.log is found then email message should be sent as "New files copied to Files folder"

Issue is my else if conditions are now working. How to achieve this?

$Username = "EmailDLxxxxx";
$Email = @("user1.gmail.com);
$Password = "";
$DesiredAttachments = @(
    "\\Server1\Files\FilesMissing.log"
    "\\Server1\Files\outputLog.log"
    "\\Server1\Files\FilesCopied.log"
    "\\Server1\Files\CAS\CAS_Folder_FilesCopied.log"
    "\\Server1\Files\CAS\CAS_Folder_FilesMissing.log"
    "\\Server1\Files\CAS\CAS_Folder_OutputLog.log"
                );

$ActualAttachments = @();
$MissingLogFiles = @();

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

   if($fileExists){
      $ActualAttachments += $LogFile
   }
   else{
      Write-output "File $LogFile was not present"
      $MissingLogFiles += $LogFile
   }
}

if($MissingLogFiles.Count -eq 0 )
{
    Send-MailMessage -From $Username -To $Email -Subject "Data Replication" `
    -Body "Dear Team, `n This is an auto generated email. Please refer to attached logs for data replication status. `n `
     FilesMissing.log -> List of missing files in Files Folder.`
     FilesCopied.log  -> Missing files copied to Files folder.`
     OutputLog.log   -> Number of files on Prod and Cob at path 'D:\inetpub\wwwroot\WebServer\Files\' `n  `
     CAS_Folder_FilesMissing .log -> List of missing files based on folder in CAS folder.`
     CAS_Folder_FilesCopied .log  -> Missing files copied to respective folders in CAS folder.`
     CAS_Folder_OutputLog.log    -> Number of files on Prod and Cob at path 'D:\inetpub\wwwroot\WebServer\Files\CAS\' `n`n Thanks `
    -Attachments $ActualAttachments -SmtpServer "xxxxxxxxx" -Port "25"
}
    # If FilesMissing.log and CAS_Folder_FilesMissing.log not generated/missing
Elseif ( (-Not ((Test-Path $DesiredAttachments[0]) -or (Test-Path $DesiredAttachments[4])))) 
{    
    write-host "1"
    Send-MailMessage -From $Username -To $Email -Subject "Data Replication" `
    -Body "Dear Team, `n This is an auto generated email. Please refer to attached output log for data replication status. `
    `n No New file(s) found on Prod server to replicate on Cob server. PROD and COB servers already in sync. `n`n Thanks  `
    -Attachments $ActualAttachments -SmtpServer "xxxxxxx" -Port "25" 
}
Elseif( ((Test-Path $DesiredAttachments[0]) -and (Test-Path $DesiredAttachments[2])) -and ( -not (Test-Path $DesiredAttachments[3]) -and (Test-Path $DesiredAttachments[4])) )
{
    Write-host "2"
    Send-MailMessage -From $Username -To $Email -Subject " Data Replication" `
    -Body "Dear Team, `n This is an auto generated email. Please refer to attached logs for data replication status. `n `
     FilesMissing.log -> List of missing files in Files Folder.`
     FilesCopied.log  -> Missing files copied to Files folder.`
     OutputLog.log   -> Number of files on Prod and Cob at path 'D:\inetpub\wwwroot\WebServer\Files\' `n  `
     NOTE: CAS Folder contents already synched, No new files found to sync CAS Folder on COB server. `
    -Attachments $ActualAttachments -SmtpServer "xxxxxxx" -Port "25"
}

Solution

  • Before doing anything else, let's run Test-Path against all the paths up front:

    $DesiredAttachments = @(
        "\\Server1\Files\FilesMissing.log"
        "\\Server1\Files\FilesCopied.log"
        "\\Server1\Files\outputLog.log"
        "\\Server1\Files\CAS\CAS_Folder_FilesMissing.log"
        "\\Server1\Files\CAS\CAS_Folder_FilesCopied.log"
        "\\Server1\Files\CAS\CAS_Folder_OutputLog.log"
    )
    $exists = $DesiredAttachments |Test-Path -PathType Leaf
    

    Now we can test for the existence of a file with just $exists[$index] instead of the much more verbose (Test-Path $DesiredAttachments[$index]) - this will likely make the conditions easier to parse and reason about.

    Now, let's have a look at your condition descriptions:

    If FilesMissing.log and FilesCopied.log not generated but log files CAS_FLDFilesMissing.log and CAS_FLDFilesCopied.log

    Okay, let's try and describe that in code:

    if (-not($exists[0] -or $exists[1]) -and $exists[3] -and $exists[4]) {
      # first condition satisfied
    }
    

    Now the second one:

    If CAS_FLDFilesMissing.log and CAS_FLDFilesCopied.log log files are not generated but FilesMissing.log and FilesCopied.log are generated

    So we just need to flip the index numbers around:

    if (-not($exists[3] -or $exists[4]) -and $exists[0] -and $exists[1]) {
      # second condition satisfied
    }