major update to override Write-Output func

This commit is contained in:
2023-10-05 13:11:38 -04:00
parent 31a06a9206
commit bbb4884818
52 changed files with 377 additions and 378 deletions
+29 -29
View File
@@ -7,14 +7,14 @@ $TimeDelta = New-TimeSpan -Days $OlderThan
# Remove all *.evtx files from system temporary files location
$EventLogs = Get-ChildItem -Path "${Env:SystemRoot}\TEMP\*" -File -Filter *.evtx
If ( $EventLogs ) {
LogMsg "Deleting all event logs from ${Env:SystemRoot}\TEMP"
Write-Output "Deleting all event logs from ${Env:SystemRoot}\TEMP"
ForEach ( $EventLog in $EventLogs ) { Remove-Item $EventLog -Force -ErrorAction SilentlyContinue }
}
# Remove misc logs and error reports
$MiscLogs = Get-ChildItem -Path "${Env:SystemRoot}\TEMP\*" -File -Include "${Env:COMPUTERNAME}-*.log", "AppxErrorReport_*.txt"
If ( $MiscLogs ) {
LogMsg "Deleting misc logs from ${Env:SystemRoot}\TEMP"
Write-Output "Deleting misc logs from ${Env:SystemRoot}\TEMP"
ForEach ( $MiscLog in $MiscLogs ) { Remove-Item $MiscLog -Force -ErrorAction SilentlyContinue }
}
@@ -25,20 +25,20 @@ If ( $CBSLogs ) {
ForEach ( $CBSLog in $CBSLogs) { $TotalBytes += $CBSLog.Length }
$TotalSize = [math]::Round(($TotalBytes / 1MB),2)
If ( $TotalSize -ge 200 ) {
LogMsg "Total CBS log archive size: ${TotalSize}MB"
Write-Output "Total CBS log archive size: ${TotalSize}MB"
Control-Service -Stop 'TrustedInstaller'
LogMsg "Deleting primary CBS.log file"
Write-Output "Deleting primary CBS.log file"
Remove-Item "${Env:SystemRoot}\Logs\CBS\CBS.log" -Force -ErrorAction SilentlyContinue
Control-Service -Start 'TrustedInstaller'
}
LogMsg "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS"
Write-Output "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS"
ForEach ( $CBSLog in $CBSLogs ) { Remove-Item $CBSLog -Force -ErrorAction SilentlyContinue }
}
# Remove all system temp files older than 7 days
$OldTempFiles = Get-ChildItem -Path "${Env:SystemRoot}\TEMP" | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles ) {
LogMsg "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:SystemRoot}\TEMP"
Write-Output "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:SystemRoot}\TEMP"
ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue }
}
@@ -62,7 +62,7 @@ ForEach($file in $RecycleBin.Items()) {
# Turn off system hibernation if it's in use
If ( Test-Path "${Env:SystemDrive}\hiberfil.sys" ) {
LogMsg "Turning off system hibernation"
Write-Output "Turning off system hibernation"
Try { Start-Process "powercfg" -ArgumentList "-h off" -Wait }
Catch { LogErr $_.Exception.Message }
}
@@ -70,7 +70,7 @@ If ( Test-Path "${Env:SystemDrive}\hiberfil.sys" ) {
# Remove PDCRevocation dump files
$PDCRevocationDumpFiles = Get-ChildItem -Path "${Env:SystemRoot}\LiveKernelReports\*" -File -Include "PDCRevocation-*.dmp"
If ( $PDCRevocationDumpFiles ) {
LogMsg "Deleting PDCRevocation dump files from ${Env:SystemRoot}\LiveKernelReports"
Write-Output "Deleting PDCRevocation dump files from ${Env:SystemRoot}\LiveKernelReports"
ForEach ( $PDCRevocationDump in $PDCRevocationDumpFiles ) { Remove-Item $PDCRevocationDump -Force -ErrorAction SilentlyContinue }
}
@@ -79,7 +79,7 @@ If ( Test-Path "${Env:ProgramData}\Genetec\Dumps" ) {
$GenetecCrashDumps = Get-ChildItem -Path "${Env:ProgramData}\Genetec\Dumps"
If ( $GenetecCrashDumps )
{
LogMsg "Deleting Genetec application crash dumps"
Write-Output "Deleting Genetec application crash dumps"
Try { ForEach ( $dump in $GenetecCrashDumps ) { Remove-Item $dump.FullName -Force -Recurse -ErrorAction SilentlyContinue } }
Catch { LogErr $_.Exception.Message }
}
@@ -89,16 +89,16 @@ If ( Test-Path "${Env:ProgramData}\Genetec\Dumps" ) {
$ESDB = "${Env:ProgramData}\Microsoft\Search\Data\Applications\Windows\Windows.edb"
If ( Test-Path $ESDB ) {
Try {
LogMsg "Found Windows Elastic Search database: ""${ESDB}"""
LogMsg " - Disabling Windows Search service"
Write-Output "Found Windows Elastic Search database: ""${ESDB}"""
Write-Output " - Disabling Windows Search service"
Start-Process "sc.exe" -ArgumentList "config wsearch start= disabled" -Wait
LogMsg " - Stopping Windows Search Service"
Write-Output " - Stopping Windows Search Service"
Start-Process "net.exe" -ArgumentList "stop wsearch" -Wait
LogMsg " - Defragmenting database file"
Write-Output " - Defragmenting database file"
Start-Process "esentutl.exe" -ArgumentList "/d ""${ESDB}""" -Wait
LogMsg " - Enabling Windows Search Service"
Write-Output " - Enabling Windows Search Service"
Start-Process "sc.exe" -ArgumentList "config wsearch start= delayed-auto" -Wait
LogMsg " - Starting Windows Search Service"
Write-Output " - Starting Windows Search Service"
Start-Process "net.exe" -ArgumentList "start wsearch" -Wait
}
Catch { LogErr $_.Exception.Message }
@@ -111,7 +111,7 @@ If ( Test-Path $ADSKInst ) {
$foldersToDelete = Get-ChildItem -Path $ADKSInst -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer) -and ($_.LastWriteTime -lt ((Get-Date) - $TimeDelta)) -and ( $_.Name -ne "WI") }
ForEach ( $folder in $foldersToDelete ) {
$path = $folder.FullName
LogMsg "Deleting Autodesk installer directory: ""${path}"""
Write-Output "Deleting Autodesk installer directory: ""${path}"""
Try { Remove-Item $folder -Force -Recurse -ErrorAction SilentlyContinue }
Catch { LogErr $_.Exception.Message }
}
@@ -125,7 +125,7 @@ ForEach ( $userProfile in $(EnumUserProfiles) ) {
# Remove all user temp files older than 7 days
$OldTempFiles = Get-ChildItem -Path "${userProfile}\AppData\Local\Temp" -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles ) {
LogMsg "Deleting all temp files not modified in ${OlderThan} day(s) from ""${userProfile}\AppData\Local\Temp"""
Write-Output "Deleting all temp files not modified in ${OlderThan} day(s) from ""${userProfile}\AppData\Local\Temp"""
ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue }
}
@@ -133,7 +133,7 @@ ForEach ( $userProfile in $(EnumUserProfiles) ) {
If (-not (IsRunning -Name "acad") ) {
$AutoCADTempFiles = Get-ChildItem -Path "${userProfile}\AppData\Local\Temp\*" -File -Include '*.ac$' -ErrorAction SilentlyContinue
If ( $AutoCADTempFiles ) {
LogMsg "Deleting AutoCAD temp files"
Write-Output "Deleting AutoCAD temp files"
ForEach ( $tempfile in $AutoCADTempFiles ) {
Try { Remove-Item $tempfile -Force -ErrorAction SilentlyContinue }
Catch { LogErr $_.Exception.Message }
@@ -167,18 +167,18 @@ ForEach ( $userProfile in $(EnumUserProfiles) ) {
# }
# If ( $IPSW ) {
# LogMsg "Found Apple software update files"
# Write-Output "Found Apple software update files"
# ForEach ( $path in $IPSW ) {
# LogMsg " - Deleting: ""${path}"""
# Write-Output " - Deleting: ""${path}"""
# Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
# Catch { LogErr $_.Exception.Message }
# }
# }
# If ( $IPA ) {
# LogMsg "Found Apple mobile application files"
# Write-Output "Found Apple mobile application files"
# ForEach ( $path in $IPA ) {
# LogMsg " - Deleting: ""${path}"""
# Write-Output " - Deleting: ""${path}"""
# Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
# Catch { LogErr $_.Exception.Message }
# }
@@ -188,10 +188,10 @@ ForEach ( $userProfile in $(EnumUserProfiles) ) {
# Remove old downloaded application files
$OldDownloadedApps = (Get-ChildItem -Path "${userProfile}\Downloads\*" -File -Filter '*.exe' -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) })
If ( $OldDownloadedApps ) {
LogMsg "Deleting old downloaded app files"
Write-Output "Deleting old downloaded app files"
ForEach ( $oldDownloadedApp in $OldDownloadedApps ) {
$path = $oldDownloadedApp.FullName
LogMsg " - Deleting: ""${path}"""
Write-Output " - Deleting: ""${path}"""
Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
Catch { LogErr $_.Exception.Message }
}
@@ -223,7 +223,7 @@ ForEach ( $userProfile in $(EnumUserProfiles) ) {
If ( Test-Path $ToDelete ) {
# Delete the duplicate file
LogMsg "Deleting duplicate download: ""${ToDelete}"""
Write-Output "Deleting duplicate download: ""${ToDelete}"""
Try { Remove-Item -Path $ToDelete -Force -ErrorAction SilentlyContinue }
Catch { LogErr $_.Exception.Message }
}
@@ -235,10 +235,10 @@ ForEach ( $userProfile in $(EnumUserProfiles) ) {
# Remove old Outlook backup files
$OldOutlookBackupFiles = Get-ChildItem -Path "${userProfile}\AppData\Local\Microsoft\Outlook\*" -File -Filter '*.bak' -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldOutlookBackupFiles ) {
LogMsg "Found Outlook backup files older than ${OlderThan} days:"
Write-Output "Found Outlook backup files older than ${OlderThan} days:"
ForEach ( $file in $OldOutlookBackupFiles ) {
$path = $file.FullName
LogMsg "Deleting: ""${path}"""
Write-Output "Deleting: ""${path}"""
Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
Catch { LogErr $_.Exception.Message }
}
@@ -248,8 +248,8 @@ ForEach ( $userProfile in $(EnumUserProfiles) ) {
# Silently run cleanmgr.exe with default settings
# TODO: This does not run silently - commenting out for now!
#Try {
# LogMsg "Running cleanmgr.exe with default settings for very low disk space"
# Write-Output "Running cleanmgr.exe with default settings for very low disk space"
# Start-Process "cleanmgr.exe" -ArgumentList "/VERYLOWDISK"
# LogMsg " - Finished"
# Write-Output " - Finished"
#}
#Catch { LogErr $_.Exception.Message }