Script to run daily check file mod date and email out

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Mavenerick
Posts: 2
Last visit: Mon Dec 21, 2020 9:50 pm

Script to run daily check file mod date and email out

Post by Mavenerick »

I need to run a scheduled task that will trigger at 7am daily and search a folder for any files that have a modified date that’s changed in the last day or 24 hours. I’m stuck on whether what I’ve done so far is the best way to do this check and also I’m not sure how to get this to email out a file with the list of file or files that have changed in the last 24 hours. I don’t think the FileSystemChecker is worth the amount of time it seems to get that running as I’ve read it can be troublesome. I’m trying to do something that just looks for files with a modified date that’s changed. I don’t have to look for deleted files or added files email the folder. If nothing has changed then I need to send the email to a different group of folks than I do if there are files that changed. I’m stuck on how to do the email part. The other part I’m stuck on is getting this to accept a unc path so I can run the task from another server.
  1. Get-Item C:\folder1\folder2*.* | Foreach { $LastUpdateTime=$.LastWriteTime $TimeNow=get-date
  2. if (($TimeNow-$LastUpdateTime).totalhours -le 24)
  3.  {
  4.  Write-Host "These files were modified in the last 24 hours "$.Name
  5.  }
  6. else
  7. {
  8. Write-Host "There were no files modified in the last 24 hours" } }
Last edited by Mavenerick on Wed Apr 22, 2020 8:43 pm, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Script to run daily check file mod date and email out

Post by jvierra »

You have explained everything you want to do but have not said what errors or issues your code is causing you.

Your code would be readable if you took some time to format it for readability.
Mavenerick
Posts: 2
Last visit: Mon Dec 21, 2020 9:50 pm

Re: Script to run daily check file mod date and email out

Post by Mavenerick »

I am not sure how to output the results to a file and send an email for the 2 situations I described. I tried doing an export-csv and that failed. The error was that no object was found.

Get-Item C:\folder1\folder2*.* | Foreach $LastUpdateTime=$.LastWriteTime $TimeNow=get-date
if (($TimeNow-$LastUpdateTime).totalhours -le 24)
{
Write-Host "These files were modified in the last 24 hours "$.Name
}
else
{
Write-Host "There were no files modified in the last 24 hours"
} }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Script to run daily check file mod date and email out

Post by jvierra »

Your syntax is bad and you have not formatted the code in any readable way.

Start by reading the following document as it will help you to understand how to write PowerShell code.

The PowerShell Best Practices and Style Guide

Also this free book can be helpful: https://www.sapien.com/books_training/W ... werShell-4
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Script to run daily check file mod date and email out

Post by jvierra »

To match a date we use:

$file.LastWriteTime - le [datetime]::Now.AddDays(-1)

Your code seems to be just a lot of copied pieces and some very bad guesses. It is hard to understand what you are asking and the code doesn't match the words.

I suggest taking the time to learn basic PowerShell before trying to write code. It will save you a lot of time.
This topic is 3 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked