PowerShell

How to create PowerShell Scheduled Jobs on the Local Computer and on Remote Hosts

Scheduled jobs do something. They do something at a predefined time or when an event occurs. You can create PowerShell scheduled jobs easily and quickly. PowerShell scheduled jobs are background jobs and they are stored in Task Scheduler. In this blog post I will show you two examples of creating PowerShell scheduled jobs. One will install a Windows server role at a specific time on the local computer and the other job creates a scheduled job on a remote computer.

Installing a Windows Server Role/Feature with Scheduled Jobs

The following one-liner will install XPS-Viewer at 05:20 p.m. on the local computer.


Register-ScheduledJob -Name InstallXPS -ScriptBlock {Install-WindowsFeature -Name XPS-Viewer} -Trigger (New-JobTrigger -Once -At 05:20pm)

Unbenannt.PNG

That looks good. Now let’s take a look at the job. Open taskschd.msc. Expand Task Scheduler Library – Microsoft – Windows – PowerShell and click on ScheduledJobs. There you’ll find your newly created job.

Unbenannt.PNG

All you have to do now is to wait until the time has come and then the task will begin installing the desired software. The time has come for me … The job status is completed.


Get-Job

Unbenannt.PNG

Creating a PowerShell Scheduled Job on a Remote Computer

Now we play through the following scenario:

  • We are logged on AzDC01
  • Our plan is to remotely create a PS scheduled job on AzMember01 (Windows Server 2016) that restarts AzMember01 at 05:15pm

Let’s hit the keys. It’s a one-liner again.


Invoke-Command -ComputerName AzMember01 {Register-ScheduledJob -Name RestartAzMember01 -ScriptBlock {Restart-Computer -Force} -Trigger (New-JobTrigger -Once -At 05:15pm)}

Unbenannt.PNG

At 05:15pm AzMember01 will perform a reboot. To make sure everything worked out fine, run Get-Job on AzMember01.


Invoke-Command -ComputerName AzMember01 {Get-Job}

Unbenannt.PNG

That’s it. As you can see, creating jobs in PowerShell is not a rocket science, it’s easy and straightforward. Have fun with it!

See also

Have a look at my other posts with Scheduled Jobs and Tasks:

Hyper-V: Backup VMs to a shared folder with Windows Server Backup and a Scheduled Task

How to schedule software installation with PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.