Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from  Instant Windows PowerShell Guide

Product type Book
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Pages 86 pages
Edition 1st Edition
Languages
Author (1):
Harshul Patel Harshul Patel
Profile icon Harshul Patel
Toc

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.

  1. 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 of Application and Security event logs with the latest 100 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)
    
  2. 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 the FilePath or ScriptBlock parameter.

  • -Authentication <AuthenticationMechanism>: This parameter explicitly provides AuthenticationMechanism for passed user credentials. The accepted values are Default, Basic, Credssp, Digest, Kerberos, Negotiate, and NegotiateWithImplicitCredential. This parameter holds the default value as Default.

  • -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 the PSCredential object type.

  • -FilePath <String>: This parameter provides the script path to the scheduled job. It uses the ArgumentList 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 the ArgumentList 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 the New-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.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013 Publisher: Packt ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}