Track updated files with a watcher task

Azure Automation uses a watcher task to look for events and trigger actions with PowerShell runbooks. The watcher task contains two parts, the watcher and the action. A watcher runbook runs at an interval defined in the watcher task, and outputs data to an action runbook.

Note

Watcher tasks are not supported in Microsoft Azure operated by 21Vianet.

Important

Starting in May 2020, using Azure Logic Apps is the recommended and supported way to monitor for events, schedule recurring tasks, and trigger actions. See Schedule and run recurring automated tasks, processes, and workflows with Azure Logic Apps.

This article walks you through creating a watcher task to monitor when a new file is added to a directory. You learn how to:

  • Import a watcher runbook
  • Create an Automation variable
  • Create an action runbook
  • Create a watcher task
  • Trigger a watcher
  • Inspect the output

Prerequisites

To complete this article, the following are required:

Import a watcher runbook

This article uses a watcher runbook called Watcher runbook that looks for new files in a directory to look for new files in a directory. The watcher runbook retrieves the last known write time to the files in a folder and looks at any files newer than that watermark.

You can import this runbook into your Automation account from the portal using the following steps.

  1. Sign in to the Azure portal.
  2. Search for and select Automation Accounts.
  3. On the Automation Accounts page, select the name of your Automation account from the list.
  4. In the left pane, select Runbooks gallery under Process Automation.
  5. Make sure GitHub is selected in the Source drop-down list.
  6. Search for Watcher runbook.
  7. Select Watcher runbook that looks for new files in a directory, and select Import on the details page.
  8. Give the runbook a name and optionally a description and click OK to import the runbook into your Automation account. You should see an Import successful message in a pane at the upper right of your window.
  9. The imported runbook appears in the list under the name you gave it when you select Runbooks from the left-hand pane.
  10. Click on the runbook, and on the runbook details page, select Edit and then click Publish. When prompted, click Yes to publish the runbook.

You can also download the runbook from the Azure Automation GitHub organization.

  1. Navigate to the Azure Automation GitHub organization page for Watch-NewFile.ps1.
  2. To download the runbook from GitHub, select Code from the right-hand side of the page, and then select Download ZIP to download the whole code in a zip file.
  3. Extract the contents and import the runbook.

Create an Automation variable

An Automation variable is used to store the timestamps that the preceding runbook reads and stores from each file.

  1. Select Variables under Shared Resources and click + Add a variable.

  2. Enter Watch-NewFileTimestamp for the name.

  3. Select DateTime for the type. It will default to the current date and time.

    Screenshot of creating a new variable blade.

  4. Click Create to create the Automation variable.

Create an action runbook

An action runbook is used in a watcher task to act on the data passed to it from a watcher runbook. You must import a predefined action runbook, either from the Azure portal of from the Azure Automation GitHub organization.

You can import this runbook into your Automation account from the Azure portal:

  1. Sign in to the Azure portal.
  2. Search for and select Automation Accounts.
  3. On the Automation Accounts page, select the name of your Automation account from the list.
  4. In the left pane, select Runbooks gallery under Process Automation.
  5. Make sure GitHub is selected in the Source drop-down list.
  6. Search for Watcher action, select Watcher action that processes events triggered by a watcher runbook, and click Import.
  7. Optionally, change the name of the runbook on the import page, and then click OK to import the runbook. You should see an Import successful message in the notification pane in the upper right-hand side of the browser.
  8. Go to your Automation Account page, and click on Runbooks on the left. Your new runbook should be listed under the name you gave it in the previous step. Click on the runbook, and on the runbook details page, select Edit and then click Publish. When prompted, click Yes to publish the runbook.

To create an action runbook by downloading it from the Azure Automation GitHub organization:

  1. Navigate to the Azure Automation GitHub organization page for Process-NewFile.ps1.
  2. To download the runbook from GitHub, select Code from the right-hand side of the page, and then select Download ZIP to download the whole code in a zip file.
  3. Extract the contents and import the runbook.

Create a watcher task

In this step, you configure the watcher task referencing the watcher and action runbooks defined in the preceding sections.

  1. Navigate to your Automation account and select Watcher tasks under Process Automation.

  2. Select the Watcher tasks page and click + Add a watcher task.

  3. Enter WatchMyFolder as the name.

  4. Select Configure watcher and choose the Watch-NewFile runbook.

  5. Enter the following values for the parameters:

    • FOLDERPATH - A folder on the Hybrid Runbook Worker where new files get created, for example, d:\examplefiles.
    • EXTENSION - Extension for the configuration. Leave blank to process all file extensions.
    • RECURSE - Recursive operation. Leave this value as the default.
    • RUN SETTINGS - Setting for running the runbook. Pick the hybrid worker.
  6. Click OK, and then Select to return to the Watcher page.

  7. Select Configure action and choose the Process-NewFile runbook.

  8. Enter the following values for the parameters:

    • EVENTDATA - Event data. Leave blank. Data is passed in from the watcher runbook.
    • Run Settings - Setting for running the runbook. Leave as Azure, as this runbook runs in Azure Automation.
  9. Click OK, and then Select to return to the Watcher page.

  10. Click OK to create the watcher task.

    Screenshot of configuring  watcher action in the Azure portal.

Trigger a watcher

You must run a test as described below to ensure that the watcher task works as expected.

  1. Remote into the Hybrid Runbook Worker.
  2. Open PowerShell and create a test file in the folder.
New-Item -Name ExampleFile1.txt

The following example shows the expected output.

    Directory: D:\examplefiles


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       12/11/2017   9:05 PM              0 ExampleFile1.txt

Inspect the output

  1. Navigate to your Automation account and select Watcher tasks under Process Automation.

  2. Select the watcher task WatchMyFolder.

  3. Click on View watcher streams under Streams to see that the watcher has found the new file and started the action runbook.

  4. To see the action runbook jobs, click on View watcher action jobs. Each job can be selected to view the details of the job.

    Screenshot of a watcher action jobs from the Azure portal.

The expected output when the new file is found can be seen in the following example:

Message is Process new file...

Passed in data is @{FileName=D:\examplefiles\ExampleFile1.txt; Length=0}

Next steps

To learn more about authoring your own runbook, see Create a PowerShell runbook.