none
Log Rotate Script RRS feed

  • General discussion

  • I need to rotate a log file "xxx.log" when it reaches specific size (10 GB), right now it's storing more than 80 GB in my E drive.

    Please let me know how to accomplish this with the below script ? I am new to PS

    Permittable limit : 30 GB
    Location : E:\IBM\WebSphere\AppServer\profiles\AppSrv01\installedApps\xxx\xxxx\xxx.war 
    File name xxx.log                                                                          

    SAMPLE SCRIPT Which I got from Net

    function Reset-Log 

        #function checks to see if file in question is larger than the paramater specified if it is it will roll a log and delete the oldes log if there are more than x logs. 
        param([string]$fileName, [int64]$filesize = 10mb , [int] $logcount = 5) 

        $logRollStatus = $true 
        if(test-path $filename) 
        { 
            $file = Get-ChildItem $filename 
            if((($file).length) -ige $filesize) #this starts the log roll 
            { 
                $fileDir = $file.Directory 
                $fn = $file.name #this gets the name of the file we started with 
                $files = Get-ChildItem $filedir | ?{$_.name -like "$fn*"} | Sort-Object lastwritetime 
                $filefullname = $file.fullname #this gets the fullname of the file we started with 
                #$logcount +=1 #add one to the count as the base file is one more than the count 
                for ($i = ($files.count); $i -gt 0; $i--) 
                {  
                    #[int]$fileNumber = ($f).name.Trim($file.name) #gets the current number of the file we are on 
                    $files = Get-ChildItem $filedir | ?{$_.name -like "$fn*"} | Sort-Object lastwritetime 
                    $operatingFile = $files | ?{($_.name).trim($fn) -eq $i} 
                    if ($operatingfile) 
                     {$operatingFilenumber = ($files | ?{($_.name).trim($fn) -eq $i}).name.trim($fn)} 
                    else 
                    {$operatingFilenumber = $null} 

                    if(($operatingFilenumber -eq $null) -and ($i -ne 1) -and ($i -lt $logcount)) 
                    { 
                        $operatingFilenumber = $i 
                        $newfilename = "$filefullname.$operatingFilenumber" 
                        $operatingFile = $files | ?{($_.name).trim($fn) -eq ($i-1)} 
                        write-host "moving to $newfilename" 
                        move-item ($operatingFile.FullName) -Destination $newfilename -Force 
                    } 
                    elseif($i -ge $logcount) 
                    { 
                        if($operatingFilenumber -eq $null) 
                        {  
                            $operatingFilenumber = $i - 1 
                            $operatingFile = $files | ?{($_.name).trim($fn) -eq $operatingFilenumber} 

                        } 
                        write-host "deleting " ($operatingFile.FullName) 
                        remove-item ($operatingFile.FullName) -Force 
                    } 
                    elseif($i -eq 1) 
                    { 
                        $operatingFilenumber = 1 
                        $newfilename = "$filefullname.$operatingFilenumber" 
                        write-host "moving to $newfilename" 
                        move-item $filefullname -Destination $newfilename -Force 
                    } 
                    else 
                    { 
                        $operatingFilenumber = $i +1  
                        $newfilename = "$filefullname.$operatingFilenumber" 
                        $operatingFile = $files | ?{($_.name).trim($fn) -eq ($i-1)} 
                        write-host "moving to $newfilename" 
                        move-item ($operatingFile.FullName) -Destination $newfilename -Force    
                    } 

                } 


              } 
             else 
             { $logRollStatus = $false} 
        } 
        else 
        { 
            $logrollStatus = $false 
        } 
        $LogRollStatus 


    Reset-Log -fileName $logfile -filesize 10mb -logcount 5

    • Changed type Bill_Stewart Friday, July 7, 2017 7:10 PM
    • Moved by Bill_Stewart Friday, July 7, 2017 7:10 PM This is not "fix/debug/rewrite my script for me" forum
    Friday, June 9, 2017 1:35 PM

All replies