Solved

PowerShell Date Check

Posted on 2013-07-01
5
443 Views
Last Modified: 2013-07-08
Hi,

i am in the process of scripting the download of McAfee updates from their FTP site.  

Using PowerShell, I have created a script which pulls down the updates and then distributes them to different filers on the network.

I am trying to implement date check logic as follows.

##Delete Old Definition Files from the Share location
remove-item "\\$dest\avupdates\neta\*.*" -force -recurse

Do {remove-item "\\$dest\avupdates\EPO\commonupdater" -force -recurse
    c:\AVUpdateScripts\wget.exe ftp://ftp.nai.com/commonupdater/ -P \\$dest\avupdates\EPO -c -x -nH -r -l 0
    $file = get-item \\$dest\avupdates\EPO\comnmonupdater\*avv.gem}
    until($file.LastWriteTime -gt (Get-Date).AddDays(-2))

I use a Do Until Loop with the date check at the end.  There are times where we get files with the incorrect date,  such as out of date.

Thanks.
0
Comment
Question by:Resonetics
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
BT15 earned 250 total points
ID: 39290852
if it were me, I would do it without the loop. you can select just the old files to delete if you wish.:


this should delete all definitions from your specified locations, download all new ones, then delete any that are more than 2 days old.
remove-item "\\$dest\avupdates\neta\*.*" -force -recurse

remove-item "\\$dest\avupdates\EPO\commonupdater" -force -recurse
c:\AVUpdateScripts\wget.exe ftp://ftp.nai.com/commonupdater/ -P \\$dest\avupdates\EPO -c -x -nH -r -l 0
get-item \\$dest\avupdates\EPO\comnmonupdater\*avv.gem | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-2)} | del -confirm:$false

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 39290865
"There are times where we get files with the incorrect date,  such as out of date."

Can you explain further what you mean by this?  It's not clear to me.
0
 

Author Comment

by:Resonetics
ID: 39290876
Yes.  Often when I go to retrieve the updated definition files, they come back with a last modified date of 11/01/2012.

Is happens intermittently.
0
 
LVL 39

Expert Comment

by:footech
ID: 39291830
If the files have a timestamp other than 11/01/2012, but a query for $file.lastwritetime returns 11/01/2012 - sorry, I've never heard of an issue like you describe.  I can't think of any way that would happen, unless it is perhaps some temporary state that the files go through when they are being created by wget.
0
 

Author Closing Comment

by:Resonetics
ID: 39308065
Very similar to the logic I submitted, but less complicated.  Thanks for the easy solution.
0

Featured Post

Does Powershell have you tied up in knots?

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A brief introduction to what I consider to be the best editor for PowerShell.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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