Weekend Scripter: Run a Scheduled Task to Check DSC

Summary: Microsoft Scripting Guy, Ed Wilson, talks about running a scheduled task to perform a consistency check for DSC.

Microsoft Scripting Guy, Ed Wilson, is here. One of the things I love to do is to automate things. Not only scripts. Not only Windows PowerShell. I also like to automate scheduled tasks. The current Task Scheduler is so much more powerful than the old-fashioned AT command that it is not funny. Well, maybe it’s funny, but it is also really cool.

One thing that is pretty neat is that Windows uses scheduled tasks to manage Windows. And I have access to the same technology. For a single scheduled task on a single machine, I usually use the GUI tool. For more information, see Use Scheduled Tasks to Run PowerShell Commands on Windows. The GUI tool t is pretty intuitive and pretty easy to use. For a truly one-off situation, it is the best tool to use (in my mind). But if I want to create the same scheduled task on a whole bunch of machines, I had better be looking at using Windows PowerShell.

In the old days, that would have meant interacting with the API (see How Can I Best Work with Task Scheduler?) or limiting myself to the AT command. The WMI classes simply interacted with the AT scheduler (see How Can I Create a Scheduled Task on a Number of Computers?), so it was not much better.

   Note  For some great information, see Using Scheduled Tasks and Scheduled Jobs in PowerShell.

Luckily, with Windows PowerShell Desired State Configuration (DSC), an automatic, consistency-check scheduled task is already present. This scheduled task is shown here:

Image of menu

The Consistency scheduled task runs every 30 minutes by default. I can see this by clicking the Triggers tab, or I can use the Get-ScheduledTask cmdlet and find the information. This technique is shown here:

PS C:\> Get-ScheduledTask -TaskName consistency | fl *

State                 : Ready

Actions               : {MSFT_TaskExecAction}

Author                : SYSTEM

Date                  :

Description           :

Documentation         :

Principal             : MSFT_TaskPrincipal2

SecurityDescriptor    :

Settings              : MSFT_TaskSettings3

Source                :

TaskName              : Consistency

TaskPath              : \Microsoft\Windows\Desired State Configuration\

Triggers              : {MSFT_TaskTimeTrigger}

URI                   :

Version               :

PSComputerName        :

CimClass              : Root/Microsoft/Windows/TaskScheduler:MSFT_ScheduledTask

CimInstanceProperties : {Actions, Author, Date, Description…}

CimSystemProperties   : Microsoft.Management.Infrastructure.CimSystemProperties


PS C:\> (Get-ScheduledTask -TaskName consistency).triggers.repetition


Duration                      Interval                                  StopAtDurationEnd PSComputerName

——–                      ——–                                  —————– ————–

                              PT1800S                                               False

Use PowerShell to run a scheduled task

Let's suppose I am checking for configuration drift and I notice that my Consistency task has recently failed as shown here:

Image of menu

I can wait for another 30 minutes and hope that it runs successfully, or I can use the GUI and try to execute the task, or I can use the Start-ScheduledTask cmdlet. The Start-ScheduledTask cmdlet requires the task name and the task path. If I attempt to run the cmdlet with only the task name, and error occurs. Luckily, I can use the Get-ScheduledTask cmdlet to retrieve the path to the scheduled task. This command is shown here:

Start-ScheduledTask -TaskName consistency -TaskPath (Get-ScheduledTask -TaskName consistency).TaskPath

The previous command and associated output are shown here:

Image of command output

I go back to the Task Scheduler GUI tool and check the history. Now I see that the Consistency task was queued, and then ran as I hoped:

Image of menu

The really cool thing is that the Start-ScheduledTask cmdlet accepts CIMSession as a parameter. This means that I can easily create a CIM session to my remote computers, and kick off the Consistency task. This technique is shown here (where I use the Get-ADComputer cmdlet to retrieve my computer names):

$cn = Get-ADComputer -Filter *

$cim = New-CimSession -ComputerName $cn.name -ea 0

Start-ScheduledTask -TaskName consistency -TaskPath (Get-ScheduledTask -TaskName consistency).TaskPath -CimSession $cim

   Note  The Get-ADComputer cmdlet comes from the Active Directory module. For more information,
   see Install Free PowerShell Remote Server Admin Tools.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (0)

Skip to main content