Solved

PowerShell Date Check

Posted on 2013-07-01
5
455 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
[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
  • 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 40

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 40

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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