Job scheduling (Intermediate)
With the release of Windows PowerShell Version 3.0, there is one dedicated module introduced for PowerShell job scheduling named PSScheduledJob
.
Getting ready
With the concept of scheduled jobs, we have the freedom to operate the data and operations as and when required. We don't need manual interaction too. PSScheduledJob
refers to an instance of a scheduled job that is started by a job trigger. There are a bunch of CMDLETs available with this module as shown:
PS C :\> Get-Command -Module PSScheduledJob CommandType Name ModuleName ----------- ---- ---------- Cmdlet Add-JobTrigger PSScheduledJob Cmdlet Disable-JobTrigger PSScheduledJob Cmdlet Disable-ScheduledJob PSScheduledJob Cmdlet Enable-JobTrigger PSScheduledJob Cmdlet Enable-ScheduledJob PSScheduledJob Cmdlet Get-JobTrigger PSScheduledJob Cmdlet Get-ScheduledJob PSScheduledJob Cmdlet Get-ScheduledJobOption PSScheduledJob Cmdlet New-JobTrigger PSScheduledJob Cmdlet New-ScheduledJobOption PSScheduledJob Cmdlet Register-ScheduledJob PSScheduledJob Cmdlet Remove-JobTrigger PSScheduledJob Cmdlet Set-JobTrigger PSScheduledJob Cmdlet Set-ScheduledJob PSScheduledJob Cmdlet Set-ScheduledJobOption PSScheduledJob Cmdlet Unregister-ScheduledJob PSScheduledJob
How to do it...
Execute the following lines of code.
The following command statement is a bunch of small subcommand statements that registers a scheduled job named
EventLogBackup
. This job executes with an elevated privilege and gets the list ofApplication
andSecurity
event logs with the latest100
entries. Moreover, this job triggers every day at 7 p.m.:PS C :\> Register-ScheduledJob -Name EventLogBackup -ScriptBlock {Get-EventLog -LogName Application, Security -Newest 100 –EntryType Error, Warning} -Trigger (New-JobTrigger -Daily -At 7pm) -ScheduledJobOption (New-ScheduledJobOption -RunElevated)
The following command statement gets
Job EventLogBackup
and clears the execution history immediately:PS C:\> Get-ScheduledJob EventLogBackup | Set-ScheduledJob –ClearExecutionHistory –RunNow
By default, the scheduled job execution history and results are stored in the following path along with the scheduled jobs:
$home\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs
How it works...
A PowerShell scheduled job is a background job that triggers at a specific time or during a recurring schedule. The Register-ScheduledJob
CMDLET creates a new scheduled job, while the Set-ScheduledJob
CMDLET changes the properties of jobs that were scheduled earlier. The following are the various parameters available with these CMDLET:
-ArgumentList <Object[]>
: This defines the values for the parameters of the script that are supplied by theFilePath
orScriptBlock
parameter.-Authentication <AuthenticationMechanism>
: This parameter explicitly providesAuthenticationMechanism
for passed user credentials. The accepted values areDefault
,Basic
,Credssp
,Digest
,Kerberos
,Negotiate
, andNegotiateWithImplicitCredential
. This parameter holds the default value asDefault
.-Credential <PSCredential>
: This parameter is to explicitly provide a credential that has permission to access the console for getting session information from the remote computer. It accepts thePSCredential
object type.-FilePath <String>
: This parameter provides the script path to the scheduled job. It uses theArgumentList
parameter to specify the parameter values to the script.-ScriptBlock <ScriptBlock>
: This provides the list of commands that a scheduled job has to run. It also uses theArgumentList
parameter to specify the parameter values to the commands.-Trigger <ScheduledJobTrigger[]>
: This parameter specifies the trigger objects to the scheduled job. The trigger objects can be retrieved using theNew-JobTrigger
CMDLET.-RunNow
: This parameter starts the execution of the scheduled job if specified. It prevents the need to create a separate trigger object and then supply it to the scheduled job explicitly. This parameter is introduced in Windows PowerShell v4.0.
Few changes in Windows PowerShell 4.0
With the beginning of PowerShell 4.0, if you execute the Get-Job
CMDLET, you will get the list of all the jobs, including scheduled jobs.
The RepeatIndefinitely
parameter has been added to New-JobTrigger
and Set-JobTrigger
that avoids the TimeSpan.MaxValue
value for the RepetitionDuration
parameter to run a scheduled job repeatedly for an indefinite period.
A Passthru
parameter has been added to the Enable-JobTrigger
and Disable-JobTrigger
CMDLETs that displays any objects that are created or modified by CMDLETs.
There's more…
There is another module that is available and that can be leveraged for managing the Web, that is, the WebAdministration
module. Invoke-RestMethod
and Invoke-WebRequest
are CMDLETs that are used here.
For example, have a look at the following command:
PS C:\ > $url=http://blogs.msdn.com/b/powershell/rss.aspx
The preceding command statement stores the PowerShell rss
link in a variable named $url
.
PS C:\ > Invoke-RestMethod -Uri $url | Select-Object Title
By using Invoke-RestMethod
, we can retrieve information from the $url
variable and further filter the output as the title list.
Note
With the beginning of Windows PowerShell 4.0, Invoke-RestMethod
and Invoke-WebRequest
now let you set all the headers by using the Headers
parameter that specifies the headers of the web request.