Logs komprimieren und automatisch löschen

PS:​

$RootLogFolder = „D:\Log“
$ArchiveSubfolder = ‚Archiv‘
$Heute = Get-Date
#delete logfiles — delete files older than 90d
$logfolders = Get-Childitem $RootLogFolder
foreach ($logfolder in $logfolders){
    If ($logfolder.exists)
    {
            Get-Childitem $logfolder.PSPath -recurse | Where-Object {($Heute – $_.LastWriteTime).Days -gt 89} | Remove-Item
    }
}
#archive and compress logfiles
foreach ($WildcardPattern in @(‚*.log.2*‘,’*_20*.log‘,’*.log-2*‘,’*.txt-2*‘)) {
    foreach ($logfile in Get-ChildItem -Recurse -LiteralPath $RootLogFolder -File -Force -Filter $WildcardPattern -Attributes !Compressed) {
        if ($logfile.Directory.Name -eq $ArchiveSubfolder) {
            continue
        }
        $DestinationFolder = Join-Path -Path $logfile.DirectoryName -ChildPath $ArchiveSubfolder
        If ((Test-Path $DestinationFolder) -eq $false){
        mkdir $DestinationFolder | Out-Null
        }
        $DestinationLogfile = Move-Item -LiteralPath $logfile.FullName -Destination $DestinationFolder -PassThru
        $cimResult = Get-CimInstance -ClassName CIM_DataFile -Filter „name=’$($DestinationLogfile.FullName -replace ‚\\‘,’\\‘)'“ | Invoke-CimMethod -MethodName Compress
    }
}