Hi, I am trying to achieve the following:
- Count the number of log files in several file paths then save the number to a txt file (along with date/time)
- Delete log files older than 90 days
- Count the number of files in the same folder then add this to the same log file
- Ideally email the log file but haven’t got that far yet
$LogLocation1="C:\logs\Archive"
$LogLocation2="C:\logs\Processed\W3SVC1"
$LogLocation3="C:\logs\W3SVC1"
$TimeStamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$LogLocationALL=$LogLocation1,$LogLocation2,$LogLocation3
$LogWriteBefore = "$TimeStamp :: $i :: LOG File Count :: $CountBefore"
$LogWriteAfter = "$TimeStamp :: $i :: LOG File Count :: $CountAfter"
Foreach ($i in $LogLocationALL)
{
$countBefore = (Get-ChildItem –Path $i -Recurse | Where-Object {($_.Extension -match "log")}|Measure-Object).count
Add-Content -Path "C:\logs\counttestlog.txt" -Value $LogWriteBefore -PassThru
Get-ChildItem –Path $i -Recurse | Where-Object {($_.LastAccessTime -lt (Get-Date).AddDays(-90) -and $_.Extension -match "log")}|Remove-Item
$CountAfter = (Get-ChildItem –Path $i -Recurse | Where-Object {($_.Extension -match "log")}|Measure-Object).count
Add-Content -Path "C:\logs\counttestlog.txt" -Value $LogWriteAfter -PassThru
}
It’s crude but it works to some extent. My issue is that the log file only seems to be showing the last folder name and count. For example
PS C:\Users\demo> C:\Tools\scripts\DeleteLOG-OlderThan90DaysV2.ps1
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
I have really struggled in the past to make anything output to txt file in a presentable manner so I am sure there are better ways to go about this.
If anyone is able to help or point me in the right direction it would be much appreciated.
Thanks
BP