PowerShell is a flexible scripting language that may be used to automate processes on Windows systems. A simple example of a PowerShell script that you may use as a starting point for many tasks is provided below.
Logs are a critical part of any application but sometimes too many unnecessary logs are generated that possibly increase the troubleshooting time for any issue plus occupy the disk space as well.
Here’s a simple PowerShell script that archives your logs inside a given path and removes the archive files after a certain period.
Before we execute the script, let’s consider an example like below where your folder n number of logs that need to be archived. Error Logs 1-7 are older than 30 days and should be moved to the Archived folder and after 90 days, it’ll be deleted. Let’s use the below script to archive and delete logs in that order.
#Declare Variables at the start of the script
-----------------------------------------------
$TIMESTAMP=get-date -format yyyyMMdd
$LOGNAME="AppLogs_$TIMESTAMP.log"
$ARCHIVE_PATH="C:\Automations\Archive"
$SOURCE_PATH="C:\Application\logs"
$ARCHIVELIST="FileList_$TIMESTAMP.txt"
$RoboCopyLog="RoboCopyLog_$TIMESTAMP.txt"
$winzippath="C:\Program Files (x86)\WinZip\winzip32.exe"
$BatchFile="C:\Scripts\winzip.bat"
$TIMESTAMP – This variable will be used to get the timestamp of the logs to archive and delete them afterward.
$LOGNAME – This stores the name of the log file that will be saved after the script is executed.
$ARCHIVE_PATH – This is where your logs will be archived.
$SOURCE_PATH – This is from where your script will pick up the logs.
$ARCHIVELIST – This is used to generate the list of files that will be archived so you can refer what files were archived.
$RoboCopyLog – This will generate the Robocopy log that you used to copy the log files temporarily.
$winzippath – This stores the path of the WinZip utility on your machine.
$BatchFile – This stores the path of the WinZip batch file on your machine to archive the logs.
#Write current date to the log file
----------------------------
Write-output $(get-date) >> $ARCHIVE_PATH\$LOGNAME
#Write an echo statement to the log file
--------------------------------------------
echo "Compressing Application log files " >> $ARCHIVE_PATH\$LOGNAME
#Creates files list from the Application logs folder which are older than 7 days that needs to be Archived
------------------------------------------------------------------------------------------------------------------
dir -path $SOURCE_PATH -recurse -name -include *.log,*.log*,*.LOG,*.LOG* | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) } >> $ARCHIVE_PATH\$ARCHIVELIST
mkdir $ARCHIVE_PATH\AppLogs_$TIMESTAMP
echo "creating a temporary copy of the log files at Archive Location" >> $ARCHIVE_PATH\$LOGNAME
robocopy $SOURCE_PATH $ARCHIVE_PATH\AppLogs_$TIMESTAMP /E >> $ARCHIVE_PATH\$RoboCopyLog
echo "Creating Zip Archive" >> $ARCHIVE_PATH\$LOGNAME
& $BatchFile $winzippath $ARCHIVE_PATH\AppLogs_$TIMESTAMP.zip $ARCHIVE_PATH\AppLogs_$TIMESTAMP
echo "Deleting temporary copy of the log files from Archive location" >> $ARCHIVE_PATH\$LOGNAME
Remove-Item -path $ARCHIVE_PATH\AppLogs_$TIMESTAMP -recurse
echo "Deleting source log files older than 7 days" >> $ARCHIVE_PATH\$LOGNAME
dir -path $SOURCE_PATH -recurse -include *.log,*.log*,*.LOG,*.LOG* | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) } | select FullName >> $ARCHIVE_PATH\$LOGNAME
dir -path $SOURCE_PATH -recurse -include *.log,*.log*,*.LOG,*.LOG* | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) } | Remove-Item
echo "Deleting archived files older than 30 days" >> $ARCHIVE_PATH\$LOGNAME
dir -path $ARCHIVE_PATH -recurse -include *.log,*.zip,*.txt | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-30) } | select FullName >> $ARCHIVE_PATH\$LOGNAME
dir -path $ARCHIVE_PATH -recurse -include *.log,*.zip,*.txt | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-30) } | Remove-Item
And you are done. This step-by-step process is pretty robust and can be used to archive any kind of logs (.txt, .log, .xcp, .err, .logx) and keep your file system clean.
Please find the entire block of code below.
#Declare Variables at the start of the script
-----------------------------------------------
$TIMESTAMP=get-date -format yyyyMMdd
$LOGNAME="AppLogs_$TIMESTAMP.log"
$ARCHIVE_PATH="C:\Automations\Archive"
$SOURCE_PATH="C:\Application\logs"
$ARCHIVELIST="FileList_$TIMESTAMP.txt"
$RoboCopyLog="RoboCopyLog_$TIMESTAMP.txt"
$winzippath="C:\Program Files (x86)\WinZip\winzip32.exe"
$BatchFile="C:\Scripts\winzip.bat"
#Write current date to the log file
----------------------------
Write-output $(get-date) >> $ARCHIVE_PATH\$LOGNAME
#Write an echo statement to the log file
--------------------------------------------
echo "Compressing Application log files " >> $ARCHIVE_PATH\$LOGNAME
#Creates files list from the Application logs folder which are older than 7 days that needs to be Archived
------------------------------------------------------------------------------------------------------------------
dir -path $SOURCE_PATH -recurse -name -include *.log,*.log*,*.LOG,*.LOG* | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) } >> $ARCHIVE_PATH\$ARCHIVELIST
#Creates a temporary copy of the logs to the Archive location
--------------------------------------------------------------------
mkdir $ARCHIVE_PATH\AppLogs_$TIMESTAMP
echo "creating a temporary copy of the log files at Archive Location" >> $ARCHIVE_PATH\$LOGNAME
robocopy $SOURCE_PATH $ARCHIVE_PATH\AppLogs_$TIMESTAMP /E >> $ARCHIVE_PATH\$RoboCopyLog
#Creates a zip archive from the temporary copy
----------------------------------------------------
echo "Creating Zip Archive" >> $ARCHIVE_PATH\$LOGNAME
& $BatchFile $winzippath $ARCHIVE_PATH\AppLogs_$TIMESTAMP.zip $ARCHIVE_PATH\AppLogs_$TIMESTAMP
#Deletes the temporary copy of the logs from the Archive location
---------------------------------------------------------------------
echo "Deleting temporary copy of the log files from Archive location" >> $ARCHIVE_PATH\$LOGNAME
Remove-Item -path $ARCHIVE_PATH\AppLogs_$TIMESTAMP -recurse
#Deletes source log files that are older than 7 days from the App folder
------------------------------------------------------------------------------
echo "Deleting source log files older than 7 days" >> $ARCHIVE_PATH\$LOGNAME
dir -path $SOURCE_PATH -recurse -include *.log,*.log*,*.LOG,*.LOG* | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) } | select FullName >> $ARCHIVE_PATH\$LOGNAME
dir -path $SOURCE_PATH -recurse -include *.log,*.log*,*.LOG,*.LOG* | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) } | Remove-Item
#Deletes files that are older than 30 days in the Archive Location
-----------------------------------------------------------------------
echo "Deleting archived files older than 30 days" >> $ARCHIVE_PATH\$LOGNAME
dir -path $ARCHIVE_PATH -recurse -include *.log,*.zip,*.txt | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-30) } | select FullName >> $ARCHIVE_PATH\$LOGNAME
dir -path $ARCHIVE_PATH -recurse -include *.log,*.zip,*.txt | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-30) } | Remove-Item
echo "Script completed successfully" >> $ARCHIVE_PATH\$LOGNAME
exit 0
Read another article on some reasons for the failure of EPM Maestro in Hyperion.
Also, to learn more about PowerShell basics, visit the tutorial on tutorialpoints.com.
Happy Coding and Keep Learning…!!!