Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

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.