by Lahru at 2012-09-20 12:07:36
I’ve been searching the web for how to do something creative with maybe Get-Date/New-Timespan but all I seem to find are ways to delete files older than x days, which is great, but …by jonhtyler at 2012-09-21 04:31:38
I’m trying to create a script for the following process:
there are 4 folders involved:
$Source
$Destination
$Backup
$File-Import-Monitor
A script runs, looking for specific files in $Source if it finds these files it copies them to $Destination and then moves them from $Source to $Backup. (Removed from $Source)
If a successful copy/move occured then a file labeled "Success.txt" is written to $File-Import-Monitor, this is done with the following command as the last portion of an If/Else statement:New-Item -Path $File-Import-Monitor -name success.txt -Value "Success!" –itemtype file -force
Since its a new-item and forced to overwrite, this ensures that the CreationTime is always accurate and newly created.
This 1st script runs every 5 minutes and if no files where copied then the Success.txt file does not get created.
What I am looking to do is write a script that will then look at $File-Import-Monitor\success.txt and compare the Creation time in minutes and if the variance is too large then it will send an email.
The e-mail would include the time variance.
So for instance: a success.txt file last got written at 12:00 pm it is now 1:35 pm This would trigger an alert set for > 90 minutes & the email would then say:
"There have been no new file imports for 95 minutes!"
I’d like to be able to set the time in minutes if at all possible so that various folders could be monitored; so say one for 90 minutes & another for 120 min etc. …
I wouldn’t think that this would be too difficult, but I’m stumped. I did find a script to delete files after 90 days and so I took this and modified it to my purposes, or so I thought.
Here is what I have so far, but I might be way off track.$a = Get-Item \my.domain.123.com\netlogon\fax-import-monitor\success.txt
$y = ((Get-Date) - $a.CreationTime).Minutes
if ($y -gt 90)
{
Send-MailMessage -From ABC@123.com -To admin@123.com -Subject "Last Import was $y ago!" -Body "Last Import was $y ago!" -SmtpServer SMTP.123.com
}
Try using .TotalMinutes instead of .Minutes.by Lahru at 2012-09-21 11:22:44
When you do the math like this, you are returning a Timespan so that you can see the DD:HH:MM:SS:mm.ticks format of the span. When you call "Minutes" property, you are pulling the minutes in relation to everything else. When you call "TotalMinutes," you get the complete value ONLY in minutes, which I think is what you want here.
Ok great, that seems to be what I needed. and this is the code I have now with the change.by DonJ at 2012-09-21 11:37:25$a = Get-Item \my.domain.123.com\netlogon\fax-import-monitor\success.txt
$b = $a.CreationTime
$y = ((Get-Date) - $a.CreationTime).TotalMinutes
if ($y -gt 90)
{
Send-MailMessage -From ABC@123.com -To admin@123.com -Subject "Last Import was at $b, $y minutes ago!" -Body "Last Import was at $b, $y minutes ago!" -SmtpServer SMTP.123.com
}
Which leads to my next question(s):
How could I then convert the value of $y into a readable time format?
I am using the value in TotalMinutes for my IF statement to ensure that if that value is greater than 90 minutes a message will be sent.
I want the users I am alerting to be able to see that it was so many Hours:Minutes ago. Normal everyday users aren’t going to want to convert minutes to hours, or even days.
My test was with a file days old and returned a message like so:
Last Import was at 09/14/2012 16:06:06, 9756.48674488 minutes ago!
If possible I would prefer that $b return a value in 12 hour format instead of 24. (e.g., 4:06:06 pm instead of 16:06:06)
If I could do that at least, then I might be able to forgo the TotalMinutes conversion.
So, if I’m reading this right, $y isn’t a DateTime anymore, it’s an integer. You just want to strip off the fraction? $y -as [int] will do so. But it’s just minutes - there’s no way to make that into a readable time format without adding it back to a date.by jonhtyler at 2012-09-21 11:42:07
$b is a DateTime; pipe it to Get-Member and you’ll find a number of methods for formatting. You probably want it as a Short Date, I’m guessing. You can also do a custom format - there’s a method for that.
Don, I think, based on playing this morning with it, that $y is now a TimeSpan, which he could use in a string formatterby Lahru at 2012-09-21 12:42:47$SomeOtherVariable = (get-date) - $a.CreationTime
"{0} days {1} hours {2} minutes" -f $SomeOtherVariable.Days, $SomeOtherVariable.Hours, $SomeOtherVariable.minutes
something to that effect.
EDIT: [sorry, forgot my dollarsigns the first run]
And I still didn’t read the question right again…you are right Don…with TotalMinutes in $y it is now an integer. But you could put the subtraction into another variable and do what I suggested above.
That is what I get for trying to read the question and answer without proper time available. Hope this didn’t make it too confusing for you.
So yes $y becomes an integerby RichardSiddaway at 2012-09-21 12:52:22
$y becomes total number of mintues between then and now.
Perhaps I need to be doing something mathematical here to convert minutes into Days:Hours:Minutes:Seconds as an interval
So instead of 9756.48674488 minutes it would read 6 days 18 hours 36 Minutes 29 Seconds, however I realize that may not be possible.
And so, I believe the following code will do as I want and I can forget about the funky math and just report when ‘IT’ happened, thanks John and Don.
Please feel free to review and let me know if you think I ought to modify something:$a = Get-Item \my.domain.123.com\netlogon\fax-import-monitor\success.txt
$b = $a.CreationTime
$y = ((Get-Date) - $a.CreationTime).TotalMinutes
$min = "90"
$LastDay = $b.ToShortdateString()
$LastTime = $b.ToShortTimeString()
if ($y -gt $min)
{
Send-MailMessage -From 'ABC@123.com' -To 'admin@123.com' -Subject "Last Fax was on $LastDay at $LastTime!" -Body "Last Fax was on $LastDay at $LastTime!" -SmtpServer 'SMTP.123.com'
}
does this do you conversionby Lahru at 2012-09-21 14:06:39
$time = 9756.48674488
$ts = New-TimeSpan -Minutes $time
"{0}:{1}:{2}:{3}" -f $ts.days, $ts.hours, $ts.minutes, $ts.seconds
I’m not certain as I am unsure how to incorporate that bit into the script.by RichardSiddaway at 2012-09-22 01:32:21
You could use it in your email