Scheduled Job not running -ScriptBlock

Have some automated tests running constantly, forever. To prune the disk space, this script removes leftover artefacts.

$trigger = New-JobTrigger -Once -At 11:00 -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration (New-TimeSpan -Hours 12) $action = { $limit = (Get-Date).AddMinutes(-5) $path = Split-Path -Path $env:temp Get-ChildItem -Path $path | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit -and $_.Name -match "scoped_dir"} | Remove-Item -Force -Recurse } $opts = New-ScheduledJobOption -RunElevated $name = "oldLogsDeleteJob" Register-ScheduledJob -Name $name -ScriptBlock $action -Trigger $trigger -ScheduledJobOption $opts 

For a reason I can’t fathom, the script block doesn’t actually run.

If I run it by itself, it runs through it’s little deleting spree. As part of the scheduled job, it doesn’t. I feel like I’m missing something simple here.

i.e.

Get-ChildItem -Path $path | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit -and $_.Name -match "scoped_dir"} | Remove-Item -Force -Recurse 

Edit to clarify The schedule “runs” per-se as Task Scheduler shows it as having run successfully. It just hasn’t run correctly and done the actual work I want it to!

submitted by /u/Midnight_Moopflops
[link] [comments]

Leave a Reply