0
votes

how to delete files older then x days and send only email notification, if files are deleted by script. Here is my script.. files are deleted by script, but email notification is not working. If I delete the if clauses (if(!$messageBody)) then email notification is working, but I am getting email when no files are deleted, too.




Get-ChildItem temp*.zip  -Path "c:\tempfiles" |
  Where-Object { -not $_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-0) }|
    ForEach-Object {

        $_ | Remove-Item -force 
    -join  $([datetime]::Now) + "     " + $_.Name| Out-File -FilePath "c:\tempfiles\logs\dellogs.txt" -Append



    $OF = "`r`n"
    $messageBody = $_.Name  + $OF  + $messageBody


    }

     $date = ( get-date )

    if(!$messageBody){


          $smtpServer = ""
          $smtpFrom = ""
          $smtpTo = ""
          $server = ""

      $messageSubject  = "Subject $server - $date"
      $body= "files"
      $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
      $smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)


     $messageBody=" "
}

1
Your code doesn't send email outside of the if(!$messageBody){ } block. Also, !$messageBody will always be False if you enter the foreach because of the newline characters. If you want to collect the file names in a list, then just create a list and add the files. Or you can just read the files from dellogs.txt. - AdminOfThings

1 Answers

0
votes

You can do the following to find temp*.zip files, delete them, log the deleted files to a log file, and then email a list of deleted files. This assumes your deletions are successful. You will need to remove the -WhatIf parameter for deletions to occur.

Get-ChildItem -Filter temp*.zip -Path "c:\tempfiles" -File -OutVariable files | Foreach-Object {
    $_ | Remove-Item -Force -WhatIf
    "{0}     {1}" -f (Get-Date),$_.Name |
        Out-File -FilePath "c:\tempfiles\logs\dellogs.txt" -Append
}
if ($files) {
    $MailParams = @{ 'To' = '[email protected]'
                     'From' = '[email protected]'
                     'Subject' = "Subject server $(Get-Date)"
                     'Body' = $files.Name | Out-String
                     'SmtpServer' = 'smtpservername'
    }
    Send-MailMessage @MailParams
}

If you want error checking and only report when files are deleted, you can make some modifications.

$files = [Collections.Generic.List[String]]@()
Get-ChildItem -Filter temp*.zip -Path "c:\tempfiles" -File | Foreach-Object {
    $_ | Remove-Item -Force -WhatIf
    if ($?) {
        "{0}     {1}" -f (Get-Date),$_.Name |
            Out-File -FilePath "c:\tempfiles\logs\dellogs.txt" -Append
        $files.Add($_.Name)
    }
}
if ($files) {
    $MailParams = @{ 'To' = '[email protected]'
                     'From' = '[email protected]'
                     'Subject' = "Subject server $(Get-Date)"
                     'Body' = $files.Name | Out-String
                     'SmtpServer' = 'smtpservername'
    }
    Send-MailMessage @MailParams
}

$? returns true if the last command executed successfully. See About_Automatic_Variables.

The syntax @MailParams is called Splatting. It is simply used here for readability.