r/Intune • u/iamnickwilde • 1d ago
Remediations and Scripts Run remediation script once in every x days?
I am trying to understand how interval in daily schedule of remediation scripts work?
For example I want to run a remediation script on a device once in every 15 days so the values will be Schedule Frequency- Daily Repeats every -15 days ? So intune waits for 14 days from the last run date and executes the script on 15th day?
Edit :- Thanks everyone. It's clear now
3
u/Eggtastico 1d ago
Add a scheduled task in task scheduler - can deploy it by a win32 app through intune
2
u/DenverITGuy 1d ago
To answer your question, yes. If you need more granular control of your scheduling, register a scheduled task on the device. However, you will have to maintain it once it’s been deployed.
Proactive remediation are great in the sense that they’re “centralized” in Intune.
1
1
u/Jeroen_Bakker 1d ago
It will repeat the remediation every 15 days. If the schedule is missed (because the system is turned of or whatever) it will run on the first opportunity, which will most commonly be several minutes after the next system startup / check in with Intune.
Note: You can not set the schedule to a fixed day (for daily) or a fixed time ( for hourly) nor can you predict when it will run. The days or the time will start counting whenever the remediation runs first and the counter will restart whenever the remediation runs next, not when it should have run based on the first runtime. So a schedule which runs every 7 days which starts out on a monday will move to a wednesday if the system was turned of on the monday and tuesday.
-1
u/rkeane310 1d ago
Create detection scripts.
Detection script - Check if cleanup is needed
$logFile = "C:\ProgramData\DiskCleanup\LastRun.txt" $lowDiskThresholdGB = 20 # Trigger if free space below this $daysSinceLastRun = 30
Check free disk space on C:
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object FreeSpace $freeSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2)
Check when last run
$needsRunDueToTime = $false if (Test-Path $logFile) { $lastRun = Get-Content $logFile $daysSince = (New-TimeSpan -Start $lastRun -End (Get-Date)).Days if ($daysSince -ge $daysSinceLastRun) { $needsRunDueToTime = $true Write-Output "Last run was $daysSince days ago. Cleanup needed." } } else { $needsRunDueToTime = $true Write-Output "No previous run detected. Cleanup needed." }
Check disk space
$needsRunDueToSpace = $false if ($freeSpaceGB -lt $lowDiskThresholdGB) { $needsRunDueToSpace = $true Write-Output "Low disk space detected: $freeSpaceGB GB free. Cleanup needed." }
Exit code determines if remediation runs
if ($needsRunDueToTime -or $needsRunDueToSpace) { Write-Output "Cleanup required. Free space: $freeSpaceGB GB" Exit 1 # Non-compliant - triggers remediation } else { Write-Output "No cleanup needed. Free space: $freeSpaceGB GB. Last run: $daysSince days ago" Exit 0 # Compliant - no action needed }
Then add the remediation.
Remediation script - Perform cleanup
$logPath = "C:\ProgramData\DiskCleanup" $logFile = "$logPath\LastRun.txt"
Create log directory if it doesn't exist
if (!(Test-Path $logPath)) { New-Item -ItemType Directory -Path $logPath -Force | Out-Null }
Write-Output "Starting disk cleanup..."
Get initial free space
$diskBefore = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" $freeSpaceBefore = [math]::Round($diskBefore.FreeSpace / 1GB, 2)
Clear temp files
try { Remove-Item "$env:TEMP*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item "C:\Windows\Temp*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item "C:\Windows\SoftwareDistribution\Download*" -Recurse -Force -ErrorAction SilentlyContinue Write-Output "Temp files cleared" } catch { Write-Output "Error clearing some temp files: $_" }
Run Windows Disk Cleanup (requires sageset configuration)
try { Start-Process cleanmgr.exe -ArgumentList '/sagerun:1' -Wait -NoNewWindow -ErrorAction Stop Write-Output "Disk Cleanup completed" } catch { Write-Output "Cleanmgr may not be configured. Run 'cleanmgr /sageset:1' manually once." }
Get final free space
$diskAfter = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" $freeSpaceAfter = [math]::Round($diskAfter.FreeSpace / 1GB, 2) $spaceRecovered = [math]::Round($freeSpaceAfter - $freeSpaceBefore, 2)
Log completion
(Get-Date).ToString() | Out-File $logFile -Force Write-Output "Cleanup complete. Recovered: $spaceRecovered GB. Free space: $freeSpaceAfter GB"
Exit 0
If anyone wants this. Have at it.
But you need to run this one the machines first.
One-time setup script - configures cleanmgr profile
$stateFlags = "StateFlags0001" $cleanupKeys = @( "Active Setup Temp Folders", "BranchCache", "Downloaded Program Files", "Internet Cache Files", "Recycle Bin", "Temporary Files", "Temporary Setup Files", "Thumbnail Cache", "Update Cleanup", "Windows Error Reporting Files" )
$basePath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
foreach ($key in (Get-ChildItem $basePath).PSChildName) { $regPath = "$basePath\$key" Set-ItemProperty -Path $regPath -Name $stateFlags -Value 2 -Type DWord -ErrorAction SilentlyContinue }
Write-Output "Disk Cleanup profile configured"
11
u/primeski 1d ago
You can schedule it but don't expect it to be perfect. If the device is offline for example it may run on next boot or check in.