major logging overhaul, plus other fixes

This commit is contained in:
2020-10-22 14:01:35 -04:00
parent 77cf4d6276
commit 978373ebe2
27 changed files with 430 additions and 611 deletions
+10 -14
View File
@@ -1,8 +1,4 @@
## Source the tools script if it isn't already available
If ( !($MSPName) ) { . $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1'))) }
## Set the name of the log file
$LogName = 'Cleanup-SystemTempFiles'
LogMsg "Cleanup-SystemTempFiles.ps1"
## Path to the system temp folder
$SysTemp = "${Env:SystemRoot}\TEMP"
@@ -14,13 +10,13 @@ $TimeDelta = New-TimeSpan -Days $OlderThan
## Only run this section if we have administrative privileges
If ( IsAdmin )
{
Log "Running with administrative privileges" -Name $LogName
LogMsg "Running with administrative privileges"
## Remove all *.evtx files from $SysTemp
$EventLogs = Get-ChildItem -Path "${SysTemp}\*" -File -Filter *.evtx
If ( $EventLogs )
{
Log "Deleting all event logs from ${SysTemp}" -Name $LogName
LogMsg "Deleting all event logs from ${SysTemp}"
ForEach ( $EventLog in $EventLogs ) { Remove-Item $EventLog -Force -ErrorAction SilentlyContinue }
}
@@ -28,7 +24,7 @@ If ( IsAdmin )
$MiscLogs = Get-ChildItem -Path "${SysTemp}\*" -File -Include "${Env:COMPUTERNAME}-*.log", "AppxErrorReport_*.txt"
If ( $MiscLogs )
{
Log "Deleting misc logs from ${SysTemp}" -Name $LogName
LogMsg "Deleting misc logs from ${SysTemp}"
ForEach ( $MiscLog in $MiscLogs ) { Remove-Item $MiscLog -Force -ErrorAction SilentlyContinue }
}
@@ -36,7 +32,7 @@ If ( IsAdmin )
$CBSLogs = Get-ChildItem -Path "${Env:SystemRoot}\Logs\CBS\*" -File -Include "CbsPersist_*.log", "CbsPersist_*.cab"
If ( $CBSLogs )
{
Log "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS" -Name $LogName
LogMsg "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS"
ForEach ( $CBSLog in $CBSLogs ) { Remove-Item $CBSLog -Force -ErrorAction SilentlyContinue }
}
@@ -45,29 +41,29 @@ If ( IsAdmin )
$OldTempFiles = Get-ChildItem -Path $SysTemp | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles )
{
Log "Deleting all temp files not modified in ${OlderThan} day(s) from ${SysTemp}" -Name $LogName
LogMsg "Deleting all temp files not modified in ${OlderThan} day(s) from ${SysTemp}"
ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue }
}
## Turn off system hibernation if it's in use
If ( Test-Path "${Env:SystemDrive}\hiberfil.sys" )
{
Log "Turning off system hibernation" -Name $LogName
LogMsg "Turning off system hibernation"
Try { Start-Process "powercfg" -ArgumentList "-h off" -Wait }
Catch { $_.Exception.Message | Log -Error -Name $LogName }
Catch { LogErr $_.Exception.Message }
}
}
## Only run this section if we don't have administrative privileges
Else
{
Log "Running without administrative privileges"
LogMsg "Running without administrative privileges"
## Remove all system temp files older than 7 days
$OldTempFiles = Get-ChildItem -Path $Env:TEMP | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles )
{
Log "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:TEMP}" -Name $LogName
LogMsg "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:TEMP}"
ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue }
}
}