Power Shell is a scripting language which has amazing capabilities to automate the administrative tasks without any intervenes. By using it, log files can be created and deleted after some duration like after 7 days, 30 days etc. This is a common task that is performed by window administrators. Let me take an example like Application logs files are created in production environment. Day by Day Size of the log folder is increased due to log files those are created on daily bases. In directly, log folder is utilizing the server space. This will affect the server performance. To clean the log folder dynamically on daily bases, Power shell script can help.
Below we will write the script to
perform same task by understanding with below mention topics.
- How can create a log file with dynamic name by using PowerShell?
- How can delete log files after 7 days by using Power Shell?
How can create a log file with dynamic name by using PowerShell?
Main problem is log files name
must be unique. So, which parameter needs to be used to make unique name. As we know that Date and Time is always
unique. So this can help.
Now Problem is which PowerShell
command is used to get the today date and time. So that is Get-Date Get-Date command will be used to get the date and time in
Format yyyymmdd_hhmmtt.
get-date
-Format yyyymmdd_hhmmtt
Now, Use the below given Script to
create the log file with unique name-
# create dynamic name
# create the file with dynamic name
$logfilename= "log_file_$(get-date -Format yyyymmdd_hhmmtt).txt"
# to validate the file name
Write-host "$lofgilename"
# Here, path can be changed as per the requirement.
$logfolderpath= "E:\LogFolder"
# To create the new log file with dynamic and unique name
New-Item -Path $logfolderpath"\log_file_$(get-date -Format yyyymmdd_hhmmtt).txt"
Run the above script. You will get the output as in figure below.
After
creating log file, Use the below command to get the created files. Output of
this command is as in figure below.
# To get the files from the log folder.
Get-childitem -Path $logfolderpath
After creating log file, need to
write the script for delete the log file from log folder after 7 days from his
creation date.
# Here, path can be changed as per the requirement.
$logfolderpath= "E:\LogFolder"
$logfiles= Get-childitem -Path $logfolderpath
foreach ($files in $logfiles)
{
Write-host $files.CreationTime.DateTime
# In if condition, User can change the days in place of 7 like 15 days then it will be like (Get-Date).AddDays(-15)
if($files.CreationTime.DateTime -eq (Get-Date).AddDays(-7) )
{
Remove-Item -Path $logfolderpath\$files
}
else
{
Write-host "No Need to delete"
}
}
Write-host "log
files for the day have been deleted successfully from log folder."
Run the above given script with your log file folder path,
it will automatically delete 7 days
older log file from the log folder.
Summary-
Both scripts are used by window server administrator or PowerShell
script developer to create the log files as well as delete the log files. To scheduling the scripts ‘Delete the log
files after 7 days’, please use the Window Task Scheduler as per your frequency
like daily or weekly. Its upon you.
I hope this blog will help you.
Comments
Post a Comment