Last active
March 11, 2018 20:10
-
-
Save Agazoth/55b90d8ce261c07d7c8bd3501afb5b4a to your computer and use it in GitHub Desktop.
A possible solution to Iron Scripter 2018 prequel. Create a scheduled job that cleans up the $env:temp folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ScheduledJobName = "CleanupTempFolder" | |
$ScriptBlock = [ScriptBlock]::Create(@' | |
[System.IO.FileInfo]$LogFile = "C:\Logs\CleanTempLog.log" | |
if (!$(Test-Path $LogFile.DirectoryName)){New-Item -ItemType Directory -Path $LogFile.DirectoryName} | |
function Get-TempFileObject { | |
param ($Timing) | |
$TempContent = Get-ChildItem $env:temp -Recurse -Force | |
[PSCustomObject]@{ | |
Filecount = ($TempContent | Where-Object {!$_.psiscontainer}).Count | |
Foldercount=($TempContent | Where-Object {$_.psiscontainer}).Count | |
TotalSizeMB=[math]::round($($TempContent | Measure-Object -Property length -Sum).Sum/1MB,2) | |
TempContent=$TempContent | |
TimeStamp = Get-Date | |
Timing=$Timing | |
} | |
} | |
$TempObject = Get-TempFileObject -Timing Before | |
$TempObject | Select-Object * -ExcludeProperty TempContent | export-csv $LogFile -Append -NoTypeInformation | |
$TempObject.TempContent.foreach{try{if($_.exists -and $_.CreationTime -lt (get-date).adddays(-1)){$_.delete()}} catch{}} | |
Clear-RecycleBin (get-item $env:temp).PSDrive -Force | |
Get-TempFileObject -Timing After | Select-Object * -ExcludeProperty TempContent | export-csv $LogFile -Append -NoTypeInformation | |
'@) | |
if (!$(Get-ScheduledJob $ScheduledJobName -ErrorAction SilentlyContinue)){ | |
Register-ScheduledJob -Name $ScheduledJobName -Trigger @{Frequency="Daily"; At="9:00 AM"} -ScriptBlock $ScriptBlock -RunNow | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex ((New-Object System.Net.WebClient).DownloadString('https://gist.github.com/Agazoth/55b90d8ce261c07d7c8bd3501afb5b4a/raw/7034fbe3cf36eab5e7f87c363ef203af97c7fd0b/CleanupTempFolder.ps1')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment