title | description | services | ms.subservice | ms.topic | ms.date |
---|---|---|---|---|---|
Track updated files with an Azure Automation watcher task |
This article tells how to create a watcher task in the Azure Automation account to watch for new files created in a folder. |
automation |
process-automation |
conceptual |
10/30/2018 |
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 Azure China Vianet 21.
Important
Starting in May 2020, using Azure Logic Apps is the 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 tutorial walks you through creating a watcher task to monitor when a new file is added to a directory. You learn how to:
[!div class="checklist"]
- Import a watcher runbook
- Create an Automation variable
- Create an action runbook
- Create a watcher task
- Trigger a watcher
- Inspect the output
To complete this tutorial, the following are required:
- Azure subscription. If you don't have one yet, you can activate your MSDN subscriber benefits or sign up for a free account.
- Automation account to hold the watcher and action runbooks and the Watcher Task.
- A hybrid runbook worker where the watcher task runs.
- PowerShell runbooks. PowerShell Workflow runbooks aren't supported by watcher tasks.
This tutorial uses a watcher runbook called Watch-NewFile 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.
This import process can be done through the PowerShell Gallery.
- Navigate to the gallery page for Watch-NewFile.ps1.
- Under the Azure Automation tab, click Deploy to Azure Automation.
You can also import this runbook into your automation account from the portal using the following steps.
- Open your Automation account, and click on the Runbooks page.
- Click Browse gallery.
- Search for Watcher runbook, select Watcher runbook that looks for new files in a directory, and click Import.
- Give the runbook a name and description and click OK to import the runbook into your Automation account.
- Select Edit and then click Publish. When prompted, click Yes to publish the runbook.
An automation variable is used to store the timestamps that the preceding runbook reads and stores from each file.
- Select Variables under Shared Resources and click + Add a variable.
- Enter Watch-NewFileTimestamp for the name.
- Select DateTime for the type.
- Click Create to create the Automation variable.
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 called Process-NewFile from the PowerShell Gallery.
To create an action runbook:
- Navigate to the gallery page for Process-NewFile.ps1.
- Under the Azure Automation tab, click Deploy to Azure Automation.
You can also import this runbook into your Automation account from the Azure portal:
- Navigate to your Automation account and select Runbooks under Process Automation.
- Click Browse gallery.
- Search for Watcher action, select Watcher action that processes events triggered by a watcher runbook, and click Import.
- Give the runbook a name and description and click OK to import the runbook into your Automation account.
- Select Edit and then click Publish. When prompted, click Yes to publish the runbook.
In this step, you configure the watcher task referencing the watcher and action runbooks defined in the preceding sections.
-
Navigate to your Automation account and select Watcher tasks under Process Automation.
-
Select the Watcher tasks page and click + Add a watcher task.
-
Enter WatchMyFolder as the name.
-
Select Configure watcher and choose the Watch-NewFile runbook.
-
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.
-
Click OK, and then Select to return to the Watcher page.
-
Select Configure action and choose the Process-NewFile runbook.
-
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.
-
Click OK, and then Select to return to the Watcher page.
-
Click OK to create the watcher task.
You must run a test as described below to ensure that the watcher task works as expected.
- Remote into the Hybrid Runbook Worker.
- 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
-
Navigate to your Automation account and select Watcher tasks under Process Automation.
-
Select the watcher task WatchMyFolder.
-
Click on View watcher streams under Streams to see that the watcher has found the new file and started the action runbook.
-
To see the action runbook jobs, click on View watcher action jobs. Each job can be selected to view the details of the job.
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}
In this tutorial, you learned how to:
[!div class="checklist"]
- Import a watcher runbook
- Create an Automation variable
- Create an action runbook
- Create a watcher task
- Trigger a watcher
- Inspect the output
Follow this link to learn more about authoring your own runbook.
[!div class="nextstepaction"] Create a PowerShell runbook