Delete files older than X days – remotely

Hi Readers,

I am sharing a script which I have written to remove logs from servers remotely.

This script run from a single server & remotely removes logs.

This method can be used for all windows related products, I have used it for sharepoint, exchange, BB

Extract the zip file, change the variables under define variables.

—————————————————————————————————————————————–

For example:-

$drive = “Y:”   (drive letter used by local server for mapping the drive from remote server)

$path = “Y:\LogFiles\W3SVC1”  (relative path of log files that needs to be removed based on age when drive is mapped)

$Age = -180 (-180 represnts 180 days old)

——————————————————————————————————————————————-

Inside function change the below highlighted letter$ (drive that will be mapped from remote server)

I have mapped e$— change it accordingly, c$,d$,f$ etc.

$net.MapNetworkDrive(“$drive”, “\\$Server\e$“)

———————————————————————————————————————————————

Now call the function in the end. (hostname01 –>server hostname) — you can call any number of servers that have logsa placed at same location.

RemoveLogs hostname01 $path $drive $Age

RemoveLogs hostname02 $path $drive $Age

RemoveLogs hostname03 $path $drive $Age

RemoveLogs hostname04 $path $drive $Age

RemoveLogs hostname05 $path $drive $Age

———————————————————————————————————————————————–

Log file will be in same folder from which script is run (under subfolder name logs)

Note:- I have added # to line $a | foreach-object{del $_.FullName -recurse}, run the script ones & check the lof to find which files will be removed then remove # & rerun the script again to delete the files.

Partial Code:-

##################################################################################################### 
##           Script to remove Log files based on age                        
##           Author: Vikas Sukhija                            
##           Date: 03-30-2012                                 
##           Modified:04-12-2012 Modified the script for all servers 
##           Modified:06/18/2013 Added variables to simplify the script. 
##################################################################################################### 

#Function remove logs created which will map the server drive & then delete operation will be executed 

Function RemoveLogs ($Server$path$drive$Age) { 

$days = (get-date).adddays($Age$date = get-date -format d 
$date = $date.ToString().Replace("/""-"$net = $(New-Object -ComObject WScript.Network) 

##########################define the drive that needs to be mapped ################################# 
########################## I have entered e$, you can use according to enviornment################## 
##########################dont only change after\\$server\ i.e e$ dont change $server############### 
##########################$server is used by function call ######################################### 

$net.MapNetworkDrive("$drive""\\$Server\e$"$aGet-ChildItem  $path | Where{$_.LastWriteTime -lt $days }  

write-host $a 

$output =  ".\" + "logs" + "\" + "$Server" + "_" + "$date" + "_log.txt" 

Add-content $output "$a log files have been deleted" 

################ remove hash # from below line after running the script first time###################  
################so that you know which files will be deleted ######################################## 

$a | Foreach-Object { del $_.FullName -recurse}
######################################################################################################

Download the Script from Below location:-

https://github.com/VikasSukhija/Downloads/blob/master/removelogs.zip

regards

Sukhija Vikas

Leave a comment