This commit is contained in:
2020-09-28 15:14:18 -04:00
parent f3b3552726
commit 54754d4d6b
3 changed files with 51 additions and 23 deletions
+16 -10
View File
@@ -1,4 +1,10 @@
## Path to the system temp folder
## 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'
## Path to the system temp folder
$SysTemp = "${Env:SystemRoot}\TEMP"
## When deleting temp files en masse, only delete files/folders older than the number of days specified here
@@ -8,13 +14,13 @@ $TimeDelta = New-TimeSpan -Days $OlderThan
## Only run this section if we have administrative privileges
If ( IsAdmin )
{
Write-Output "Running with administrative privileges"
Log "Running with administrative privileges" -Name $LogName
## Remove all *.evtx files from $SysTemp
$EventLogs = Get-ChildItem -Path "${SysTemp}\*" -File -Filter *.evtx
If ( $EventLogs )
{
Write-Output "Deleting all event logs from ${SysTemp}"
Log "Deleting all event logs from ${SysTemp}" -Name $LogName
ForEach ( $EventLog in $EventLogs ) { Remove-Item $EventLog -Force -ErrorAction SilentlyContinue }
}
@@ -22,7 +28,7 @@ If ( IsAdmin )
$MiscLogs = Get-ChildItem -Path "${SysTemp}\*" -File -Include "${Env:COMPUTERNAME}-*.log", "AppxErrorReport_*.txt"
If ( $MiscLogs )
{
Write-Output "Deleting misc logs from ${SysTemp}"
Log "Deleting misc logs from ${SysTemp}" -Name $LogName
ForEach ( $MiscLog in $MiscLogs ) { Remove-Item $MiscLog -Force -ErrorAction SilentlyContinue }
}
@@ -30,7 +36,7 @@ If ( IsAdmin )
$CBSLogs = Get-ChildItem -Path "${Env:SystemRoot}\Logs\CBS\*" -File -Include "CbsPersist_*.log", "CbsPersist_*.cab"
If ( $CBSLogs )
{
Write-Output "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS"
Log "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS" -Name $LogName
ForEach ( $CBSLog in $CBSLogs ) { Remove-Item $CBSLog -Force -ErrorAction SilentlyContinue }
}
@@ -39,29 +45,29 @@ If ( IsAdmin )
$OldTempFiles = Get-ChildItem -Path $SysTemp | Where { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles )
{
Write-Output "Deleting all temp files not modified in ${OlderThan} day(s) from ${SysTemp}"
Log "Deleting all temp files not modified in ${OlderThan} day(s) from ${SysTemp}" -Name $LogName
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" )
{
Write-Output "Turning off system hibernation"
Log "Turning off system hibernation" -Name $LogName
Try { Start-Process "powercfg" -ArgumentList "-h off" -Wait }
Catch { Write-Output $_.Exception.Message }
Catch { $_.Exception.Message | Log -Error -Name $LogName }
}
}
## Only run this section if we don't have administrative privileges
Else
{
Write-Output "Running without administrative privileges"
Log "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 )
{
Write-Output "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:TEMP}"
Log "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:TEMP}" -Name $LogName
ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile -Force -Recurse -ErrorAction SilentlyContinue }
}
}