Sometime we need some job or trigger to execute at specific time. We can trigger this by using powershell cmdlets. With new version of Powershell, we get cmdlets for scheduling triggers at time required. Job scheduling allows you to schedule execution of a windows powershell background job for a later time. First thing you do is create a job trigger. This defines when the job will execute.
You need to import PSScheduledJob first then associated cmdlets would be available.
You can import module by command
Import-Module PSScheduledJob
and check associated cmdlets by
Get-Command -Module *PSScheduled*
For creating job, you need to use New-JobTrigger(trigger) cmdlet and need to register the job with specific schedule and command.
$Newtrig=New-JobTrigger -Daily -At 7PM # This is new trigger which we can use while registering Job
Register-ScheduledJob -Name Copyfile -trigger $Newtrig -ScriptBlock {Copy-Item D:\Shanky\ "\\10.89.0.9\Shanky\Daily" -Recurse}
Once the trigger has fired and the job has run, You can work with it the same way you do with regular background jobs.
Get-Job -Name Copyfile | Receive-Job
You can start a scheduled job manually, rather than waiting for the trigger to fire:
Start-job -DefinitionName Copyfile
| Other cmdlets available from PSScheduledJob |
Good article
ReplyDelete