Cleaning the Temp folder - Scheduling
When I posted about cleaning the temp folder http://msmvps.com/blogs/richardsiddaway/archive/2009/11/04/cleaning-temp-folder.aspx I said I would look at scheduling the task. I had intended to use the TaskScheduler module in the new PowerShell pack which can be downloaded from http://code.msdn.microsoft.com/PowerShellPack. It is also available in the Windows 7 Resource Kit. The pack provides 10 modules for PowerShell 2.0 covering a number of areas:
DotNet
FileSystem
IsePack
PowerShellPack
PSCodeGen
PSImageTools
PSRSS
PSSystemTools
PSUserTools
TaskScheduler
WPK
The modules are installed in WindowsPowerShell\Modules in your profile.
I’ll look at some of the others in future posts but for now I want to concentrate on the Scheduler. The module supplies a number of functions
Add-TaskAction
Add-TaskTrigger
Connect-ToTaskScheduler
Get-RunningTask
Get-ScheduledTask
New-Task
Register-ScheduledTask
Remove-Task
Start-Task
Stop-Task
I had difficulty with the Add-TaskAction and Add-TaskTrigger functions so I ended up experimenting and produced the following script
| 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026
| Import-Module TaskScheduler $task = New-Task $at = [datetime]"1:00 PM" ## trigger on daily schedule $trigger = $task.Triggers.Create(2) $trigger.StartBoundary = $at.ToString("s") $trigger.DaysInterval = 1 $task ## run command line operation $Action = $Task.Actions.Create(0) $action.Path = Join-Path $psHome "PowerShell.exe" $action.WorkingDirectory = $pwd $Script = [ScriptBlock]{Import-Module FileFunctions; Remove-TempContents} $encodedScriptBlock = $encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($script)) $action.Arguments+= " -encodedCommand $encodedCommand" $task $scheduler = Connect-ToTaskScheduler $scheduler.Connected $folder = $scheduler.GetFolder("") ## arguments task name, task definition, create task, userid, password, user must be logged in, security descriptor $folder.RegisterTaskDefinition("TempClean", $task, 6, "", "", 3, $null) |
I imported the TaskScheduler module and used the New-Task function to create a task. I then set the task to run every day at 1:00pm. The action the task performs is to first start PowerShell in the present working directory. Create a script block to run and encode it as shown. The two references to $task show me the contents of the task variable. its worth a look especially the XML.
Connect-TaskScheduler is used to create a variable I can use for scheduling the task.
We can see the scheduled task using
Get-ScheduledTask
We can run the task outside of the scheduled time
PS> Get-ScheduledTask -Name TempClean | Start-Task
Name : TempClean
InstanceGuid : {218CB104-5FBF-4611-B02D-151AA20363C1}
Path : \TempClean
State :
CurrentAction : C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe
EnginePID : 1948
And we can remove the task
Get-ScheduledTask -Name TempClean | Remove-Task
the task scheduler API is documented on MSDN. http://msdn.microsoft.com/en-us/library/aa383614(VS.85).aspx
This holds a lot of possibilities including creating tasks on remote machines. Needs more investigation especially why the functions aren’t working for me.