Enhance logging format for powershell output

samiam41
samiam41 used Ask the Experts™
on
Hey Experts!  As I evolve my script, I wanted to improve the log file format.  I need to help with the programming portion.  

Right now the script will delete the files that meet the criteria set and output basic info to a log file.

$logfile = "C:\Tools\Logs\SW_Cleanup.log"
cd "C:\tools\Scripts\Test"

forfiles /d -0 /c "cmd /c echo @path @fdate" > $logfile
forfiles /d -0 /c "cmd /c del /q /f @file"

Open in new window


I would like to include a header like this:

$scriptversion = 10.12.18


Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]."
Add-Content -Path $sFullPath -Value "***************************************************************************************************"
Add-Content -Path $sFullPath -Value ""
Add-Content -Path $sFullPath -Value "SpiceWorks purge of backups older than 10 days"
Add-Content -Path $sFullPath -Value ""
Add-Content -Path $sFullPath -Value "Running script version [$ScriptVersion]."
Add-Content -Path $sFullPath -Value ""
Add-Content -Path $sFullPath -Value "***************************************************************************************************"
Add-Content -Path $sFullPath -Value "

Through my testing, I've tried various add-content and write-log options with no success so I figured it was time to ask the professionals.  I appreciate your time and assistance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
$StartFolder = "C:\tools\Scripts\Test"
$logfile = "C:\Tools\Logs\SW_Cleanup.log"
$version = "10.12.18"

@"
**************************************************
Purge of SpiceWorks backups older than 10 days

Ran on: $(Get-Date -format 'yyyy-MM-dd HH:mm:ss')
**************************************************

**************************************************
Running script version $version
**************************************************

"@ | Out-File $logfile

#

dir $StartFolder -Recurse -Force -ea 0 | 
?{!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-0)} | 
ForEach-Object {
	$_ | del -Force
	$_.FullName | Out-File C:\Tools\Logs\SW_Cleanup.log -Append

}

Open in new window


I used -0 days so that I could test out the script and delete out any files 0 days old (essentially all).  I'm changing it back to -10 so it will only delete those files that are older than that.
Commented:
See my post above.

Thanks anyways Experts!  Catch up with you next time.
Top Expert 2016
Commented:
$Scriptversion = '10.12.18'
$logfile = "C:\Tools\Logs\SW_Cleanup.log"
$path = 'C:\tools\Scripts\Test'
$now = get-date -Format "dd/MMM/yy hh:mm"
$log = @()
$log += "Started processing at [$([DateTime]::Now)]."
$log +=  "***************************************************************************************************"
$log += " "
$log +=  "SpiceWorks purge of backups older than 10 days"
$log += "Running script version [$ScriptVersion]."
$log += " "
$log += "***************************************************************************************************"
$log += " "
$deletedfiles = 0
$files = get-childitem -Path $path
$10days = (get-date).AddDays(-10)
#$10days = (get-date).AddDays(-0)
foreach ($file in $files){
  if ($file.LastWriteTime -le $10days){
    remove-item -Path $file.fullname -Force
    $log += "Deleted:" + $file.name
    $deletedfiles ++

  }
}
$log += "Deleted: " + $deletedfiles + " Files"
$log | out-file $logfile
$log

Open in new window

Author

Commented:
Actually going to take parts out of your script and incorporate into mine so I thought it only fair to include you in the answer process.  Thank you for your time and script!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial