Jump to content
Shadoxity

LT Cache Cleanup script

Recommended Posts

Hey Guys,

 

I have put together a little script to clean up the labtech cache. I found that on some of our sites these were getting to 20+gb and there is a lot of old updates etc in there.

This took our local cache from 12gb down to 3gb.

 

The script deletes any files from the cache that are older than 60 days.

If you want to modify this time frame you can change it on line 8 of the script - $limit = (Get-Date).AddDays(-60) < Enter your number here

 

 

The tricky part is deciding what to run this on. Maybe run it against the domain controller, or the network probe.

If anyone has some good ideas on how to run this for each site, I'd love to know.

 

You can get the script in the script exchange.

 

*ADMIN EDIT*

Use CTaylors on post #5

*/Edit*

Share this post


Link to post
Share on other sites

Nice one. A couple of additions/notes:

 

1. Obviously you need to download a copy of diruse.exe (http://support.microsoft.com/kb/927229) and put it in the Transfer/tools folder.

2. The scrpit fails on the download - gets stuck on this part for us, but if the folder/file is in place it works.

 

I will look into creating a extra data field/Search/Group for LTCache severs so that this can run be scheduled to run against the LTCache server only. Will also see why it gets stuck on the download. Will get back in the next few days.

Share this post


Link to post
Share on other sites

For some reason I could not get diruse.exe to copy across - AV or blocked by Windows - don't know. Ended up using the sysinternals bu.exe instead, but this will not work with the %cachedir% as it is a UNC path. https://technet.microsoft.com/en-gb/sysinternals/bb896651.aspx

 

This works:

%ltsvcdir%\Tools\du.exe -ct c:\ltcache -accepteula

...but a bit clumsy. I did a couple of If FILE Exists to look for c:\ltcache and then d:\ltcache and then run as appropriate.

 

It may be possible to ZIP and copy over diruse.exe as looks a better tool. Will come back to this when I have some spare time.

 

The PowerShell element to the script also works for older than 60 days BTW - not 3 months. Not a problem though and can be edited of course.

 

Next bit to see how we can automate this.

Share this post


Link to post
Share on other sites

The work around I came up with uses just the shell as admin after using SQL to format the UNC path, the pro is that it can be run against any system on the network, con is it leaves empty folders in the directory.

 

Set: @CacheDirectory@ = MYSQLENCODE(%CasheDir%)

-- Script String Functions

-- Operation: MYSQL Encode

-- Original String: %CacheDir%

Set: @CacheDirectory@ = SQLResult ....

-- Variable Set

-- Set type: SQL Query

-- Parameter: SELECT CONCAT("\\\\", SUBSTRING(REPLACE("@CacheDirectory@", (SUBSTRING('@CacheDirectory@', LOCATE("|",'@CacheDirectory@'))) ,""),3));

-- Variable Name: CacheDirectory

Shell As Admin:

-- PushD @CacheDirectory@ &&(forfiles -s -m * -d -60 -c "cmd /c del @path /q") &&popd

 

The 60 above is for 60 days, just modify to 30 for 1 month retention, and %cachedir% is an EDF under the locations General tab named 'Drive' should look something like: \\SERVER\Cache .

Share this post


Link to post
Share on other sites

WARNING: The script listed above will delete all the files it can from the System32 directory if there is no location drive set.

 

I have modified the script to validate %locationdrive% and make sure that there is a UNC path set.

I also run this against the following search that also makes sure that a UNC path is set.

dVsvB9y.png

LT Cache folder cleanup.zip

Share this post


Link to post
Share on other sites

Would an alternative solution be to add the cache directory to the list of directories in the Disk Cleanup tab in the Dashboard?

  • Like 1

Share this post


Link to post
Share on other sites
On 1/19/2018 at 3:17 PM, lgs141 said:

Would an alternative solution be to add the cache directory to the list of directories in the Disk Cleanup tab in the Dashboard?

This is an interesting idea. The problem would be making it delete the subfolders as well as files. I don't see any way to make that happen.

Share this post


Link to post
Share on other sites
On 12/02/2018 at 11:32 PM, plarue said:

This is an interesting idea. The problem would be making it delete the subfolders as well as files. I don't see any way to make that happen.

I tested and can confirm the cachedir variable does not work within the disk cleanup section. The disk cleanup log files make no mention of it when the job runs. We've restored to a monitor to alert us when disk space usage is high.

Share this post


Link to post
Share on other sites

If you have semi-standardized LTCache path (D:\LTCache, E:\LTCache, etc) it would be pretty simple to add definitions to Disk Cleanups for those paths.

I made a modified version of the script that does everything through powershell rather than using a third-party tool. I also added logic for cache drives with a different drive letter piped in. Original script couldn't handle something like \\server\ltcache|W: like we have at a few locations. Tested on 2008R2 and 2012R2.

LTCache Cleanup (Powershell).xml

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×