简体   繁体   中英

Looking for advice on how to proceed with working powershell script

So I posted for and got some help with this script:

#Command to get list of folders with logfiles where the logfile is at least 30 minutes old send results to variable.
$varlogfile = Get-ChildItem -Path "drive:\folder" -Recurse -Include "logfile" | Where-Object {$_.LastWriteTime -le ((Get-Date).AddMinutes(-30))}

#Add a carriage return to results contained in the variable so email is easier to read
$varlogfile =  $varlogfile -join "`r`n"

#Email setup from this line down to next comment
$SMTPServer = "email.server"
$From = "Administrator <administrator@place.com>"
$To = "email","email2"
$Subject = "A Logfile older than 30 minutes has been detected"
$Body = "Logfile(s) older than 30 minutes have been detected in the following folder(s):

$varlogfile

Please login and attempt to process the files manually, if the manual process fails, open a ticket with someone.

From the Admin
"
#Email setup above this line

#If statement that looks for the text blah in the variable, if found email is sent.
if($varlogfile -match "blah")
{
    #Command to send email
    Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer
}

exit 0;

And all that is working perfectly.

Here's the thing though. Over the weekend sometimes we may get a stuck logfile that can't be resolved until Monday morning and it would be nice to be able to turn off alerts when this happens.

Now I'm very new to powershell and this script has been my learning experience. The way I see it is I have 3 choices:

  1. Keep the get-childitem from returning a result if it sees logfile and logfile.stop.

  2. After get-childitem has produced $varlogfile, search $varlogfile for logfile.stop and delete the lines logfile and logfile.stop from it.

  3. Rewrite the whole thing from scratch and produce $varlogfile in a better way that makes it easier to work with the results.

Thoughts and opinions? I'm leaning toward method 2, as I think I can figure that out, but I'm curious if that is a way of pain. I'd really like your input on this.

Thanks people!

I think you're on the right path with your current plan, so I'll help you with approach #2, creating a .sent file when we send an email, to keep the emails from sending multiple times.

Our first step: When an e-mail is sent , we create a new file titles $logfile.MessageSent or something like that. Doing this allows an e-mail to be sent, and for us to also create a flag that we can search for later in the filesystem to determine whether or not we send another e-mail.

#If statement that looks for the text blah in the variable, if found email is sent.
if($varlogfile -match "blah")
{
#Command to send email
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer
New-Item -path $varLogfile.Sent -itemType File
}

Our second step: Modify our Get-ChildItem query to search for the flag:

$varlogfile = Get-ChildItem -Path "drive:\folder" -Recurse -Include "logfile"  | 
Where-Object {$_.LastWriteTime -le ((Get-Date).AddMinutes(-30))} | 
? "($_.BaseName).sent" -notin (Get-ChildItem -Recurse -Include "*.sent" -Path "drive:\folder" | Where-Object {$_.LastWriteTime -le ((Get-Date).AddMinutes(-30))})

This second modification to the $varlogfile step is hard to understand, admittedly. Here is how I've changed it:

  • Get a lit of files in the drive\\folder path, recursively and include logfile
  • Where the LastWriteTime is older than 30 mins
  • Where filename.sent is not found in the same directory

The only other thing you'll need to do is add a cleanup task to regularly delete the .sent files, and you're good to go.

Please let me know if you have any questions about this approach, as I want to be sure you understand and to help you learn.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM