Solved

Need eloquent PowerShell script to get oldest file in a directory

Posted on 2008-06-10
12
2,169 Views
Last Modified: 2010-05-18
I'm creating a script to do the following:
Go into the default log file location for Exchange 2003
Get the list of files held inside
If it's E00*.log, I care about it.
If it's res1.log or res2.log or E00tmp.log, then I don't want it.
It grabs the oldest out of the pile of the E00's and grabs the current date.
If the current file is dated before today, then send me an email saying "Your Log files are old".
If it's not, send me an email saying "Your log files are currently within the nominal time frame".
Also it does a count of the entire directory.


So now that you've gotten the background, all I need help with is the code where it's supposed to get that actual one file into usable format.

I can do something like the code listed below

But the problem is it keeps giving me horrible output.  I can get it to sort and format-table no problem.  I just feel like I'm missing the big picture here.  It should be "If it's not this or this, grab the date, stuff it into the variable"

That's all I need.  Anyone think they can help me out?

Also, may I sa
$directory = C:\Scripts\*.log
$oldestfile = dir $directory | sort -property lastwritetime | select -first 3
"$oldestfile"

Open in new window

0
Comment
Question by:Dale Harris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 18

Expert Comment

by:BSonPosh
ID: 21753405
what exactly do you want it to look like?

0
 
LVL 3

Expert Comment

by:patrickfromsc
ID: 21753499
I apologize for digressing, but you don't want to delete log files, you want to run an online backup and flush them.  NTBackup on the Exchange Server can do this online backup and flush the logs, and can be scheduled to run nightly.

Regards,
PfSC
0
 
LVL 18

Accepted Solution

by:
BSonPosh earned 500 total points
ID: 21753520
Try this

$logFiles = get-childitem <path> | ?{$_.Name -match "E\d\d(?!(.*tmp))"}
$oldfiles = $logFiles | sort -desc LastWriteTime | ?{$_.LastWriteTime -lt (Get-Date).addhours(-24)
if($oldfiles){"Your {0} log files are currently within the nominal time frame." -f $oldfiles.count}
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 16

Author Comment

by:Dale Harris
ID: 21754102
BSonPosh (Bachelors of Science on Powershell?)
Does that mean you're a closet Microsoft MVP :)
I bet you're Bruce Payette himself.
Anyways, back to the problem:
I've also got to exclude *res* files.  Currently, it doesn't work.  But I think once you exclude res files from the process, it should work.  I created an else after your last line that says "Your files are old".  

Also, can I ask for you to show me exactly what you did...

I've got the meat and potatoes of it

? = if statement
$_.Name = every file .name

"E\d\d(xxxxxxxx)" = ??  What's the \d's, the ?! = if is not equal to I'm assuming and why is there a period before the asterisk and then tmp.  How does that work?

-Dale

P.S. Thanks for the quick response!
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 21754208
1) I am not a closet MVP.. its in my profile :)
2) BS = my intials (how cool right?)
3) Not Bruce :)
4) "E\d\d(?!(.*tmp))" is a regex... It should be any file that has E{digit}{digit} and number of characters but not tmp

\d = decimal
?| = Negative Lookahead
.* = any number of characters.

Try this
"^E\d\d(?!(.*tmp))"


0
 
LVL 16

Author Comment

by:Dale Harris
ID: 21754367
What about the *res* files.  I'll need to do something like "E\d\d(?!(.*tmp) -or (.*res))"

Will that work?

I'll have to check out your profile :)

0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 21754383
you do NOT want Res or do want Res?

The regex with Get-Childitem should already exclude them.
0
 
LVL 16

Author Comment

by:Dale Harris
ID: 21754514
Well my .log files were already within normal limits, but it definitely told me they were old.  I can only assume that it was counting another file (res files).  I don't want them counted.  I can double check the output.  Thanks again for the quick help.

-Dale
0
 
LVL 16

Author Comment

by:Dale Harris
ID: 21754639
Okay I ended up adding

?!(.*tmp -or .*res)
and it didn't throw any errors.

Then I changed the -lt on the last line to -gt and it basically makes sure the log files are within 24 hours of the time I'm running the script.  Saying "They are fresh".

Thanks a lot.

-Dale Harris
0
 
LVL 16

Author Closing Comment

by:Dale Harris
ID: 31465853
Very quick, custom and efficient.  You gave me all I asked for and then some.  Thanks a lot for also going into detail and explaining it so I can actually learn from this experience.
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 21754679
the -or wont work... that is a Powershell operator not a regex operator.

Try this
"^E\d\d(?!(.*(tmp|res)))"
0
 
LVL 16

Author Comment

by:Dale Harris
ID: 21762324
It works great.  Thanks.  Also, you've got quite a nice blog/website.  It motivates me to get better at Powershell.  I'll be picking up the book written by Bruce Payette.  Thanks again for all the help.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question