Save this few line in a DeleteOldLogs.ps1 file.
- param ($FolderPath, $Debug)
- if ($debug -ne "true"){$debug = [bool]$false}else{$debug = [bool]$true}
- $Script:API = new-object -comObject "MOM.ScriptAPI"
- $Script:Bag = $Script:API.CreatePropertyBag()
- $Script:LOG_ERROR = 1
- $Script:LOG_WARNING = 2
- $Script:LOG_INFORMATION = 4
- $Script:ScriptName = "DeleteOldLogs.ps1"
- $Script:Arguments = "Received Arguments:`FolderPath = $FolderPath `rDebug = $debug"
- function Write-DebugInfo([string] $msg)
- {
- if ($debug)
- {
- $Script:API.LogScriptEvent("$ScriptName",100,$Script:LOG_INFORMATION,"`r$Arguments`r`r$msg")
- }
- }
- function Write-ErrorInfo([string] $msg)
- {
- $Script:API.LogScriptEvent("$ScriptName",500,$Script:LOG_ERROR,"`r$Arguments`r`r$msg")
- }
- $date= (get-date).AddMonths(-1)
- $Files = get-childitem -Path $FolderPath -include *.log -recurse | Where-Object {$_.CreationTime -lt $date}
- foreach ($File in $Files)
- {
- if ($File) {
- Write-DebugInfo "Deleting File '$File'"
- Remove-Item $File | out-null
- }
- }
In the powershell command shell, just launch
> DeleteOldLogs.PS1 "C:\temp" "true"
This posting is provided "AS IS" with no warranties.
No comments:
Post a Comment