您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

创建 Azure 自动化观察程序任务来跟踪本地计算机上的文件更改Create an Azure Automation watcher tasks to track file changes on a local machine

Azure 自动化通过 PowerShell Runbook 使用观察程序任务来监视事件并触发操作。Azure Automation uses watcher tasks to watch for events and trigger actions with PowerShell runbooks. 本教程引导你创建一个观察程序任务来监视何时向目录中添加了新文件。This tutorial walks you through creating a watcher task to monitor when a new file is added to a directory.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 导入观察程序 runbookImport a watcher runbook
  • 创建自动化变量Create an Automation variable
  • 创建操作 runbookCreate an action runbook
  • 创建观察程序任务Create a watcher task
  • 触发观察程序Trigger a watcher
  • 检查输出Inspect the output

必备组件Prerequisites

完成本教程需要以下各项:To complete this tutorial, the following are required:

备注

在 Azure 中国区不支持观察程序任务。Watcher tasks are not supported in Azure China.

导入观察程序 runbookImport a watcher runbook

本教程使用名为 Watch-NewFile 的观察程序 runbook 查找某个目录中的新文件。This tutorial uses a watcher runbook called Watch-NewFile to look for new files in a directory. 该观察程序 runbook 检索文件夹中的文件的最后已知写入时间,并查找比该水印更新的任何文件。The watcher runbook retrieves the last known write time to the files in a folder and looks at any files newer than that watermark. 在此步骤中,你将此 runbook 导入到你的自动化帐户中。In this step, you import this runbook into your automation account.

  1. 打开你的自动化帐户,然后单击“Runbook”页。 Open your Automation account, and click on the Runbooks page.
  2. 单击“浏览库”按钮。 Click on the Browse gallery button.
  3. 搜索“Watcher runbook”,在目录中选择“Watcher runbook that looks for new files in a directory” ,然后选择“导入”。 Search for "Watcher runbook", select Watcher runbook that looks for new files in a directory and select Import. 从 UI 导入自动化 runbookImport automation runbook from UI
  4. 为该 runbook 提供名称和说明,然后选择“确定” 以将该 runbook 导入到自动化帐户中。Give the runbook a name and description and select OK to import the runbook into your Automation account.
  5. 选择“编辑” ,然后单击“发布” 。Select Edit and then click Publish. 出现提示时,选择“是” 以发布该 runbook。When prompted select Yes to publish the runbook.

创建自动化变量Create an Automation variable

使用一个自动化变量来存储前面的 runbook 从每个文件读取和存储的时间戳。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 select + Add a variable.
  2. 输入“Watch-NewFileTimestamp”作为名称Enter "Watch-NewFileTimestamp" for the name
  3. 选择“日期时间”作为类型。Select DateTime for Type.
  4. 单击“创建”按钮。 Click on the Create button. 这将创建该自动化变量。This creates the automation variable.

创建操作 runbookCreate an action runbook

在观察程序任务中,操作 runbook 用来对从观察程序 runbook 传递给它的数据执行操作。An action runbook is used in a watcher task to act on the data passed to it from a watcher runbook. 观察程序任务不支持 PowerShell 工作流 Runbook,必须使用 PowerShell Runbook。PowerShell Workflow runbooks are not supported by watcher tasks, you must use PowerShell runbooks. 在此步骤中,你将更新一个名为“Process-NewFile”的预定义的操作 runbook。In this step, you update import a pre-defined action runbook called "Process-NewFile".

  1. 导航到你的自动化帐户,从“流程自动化”类别下选择“Runbook”。 Navigate to your automation account and select Runbooks under the PROCESS AUTOMATION category.
  2. 单击“浏览库”按钮。 Click on the Browse gallery button.
  3. 搜索“Watcher action”,在目录中选择“Watcher action that processes events triggered by a watcher runbook” ,然后选择“导入”。 Search for "Watcher action" and select Watcher action that processes events triggered by a watcher runbook and select Import. 从 UI 中导入操作 runbookImport action runbook from UI
  4. 为该 runbook 提供名称和说明,然后选择“确定” 以将该 runbook 导入到自动化帐户中。Give the runbook a name and description and select OK to import the runbook into your Automation account.
  5. 选择“编辑” ,然后单击“发布” 。Select Edit and then click Publish. 出现提示时,选择“是” 以发布该 runbook。When prompted select Yes to publish the runbook.

创建观察程序任务Create a watcher task

观察程序任务包含两个部分。The watcher task contains two parts. 观察程序和操作。The watcher and the action. 观察程序以观察器任务中定义的间隔运行。The watcher runs at an interval defined in the watcher task. 来自观察程序 runbook 的数据被传递到操作 runbook。Data from the watcher runbook is passed onto the action runbook. 在此步骤中,你将配置引用在前面的步骤中定义的观察程序和操作 runbook 的观察程序任务。In this step, you configure the watcher task referencing the watcher and action runbooks defined in the preceding steps.

  1. 导航到你的自动化帐户,从“流程自动化”类别下选择“观察程序任务”。 Navigate to your automation account and select Watcher tasks under the PROCESS AUTOMATION category.

  2. 选择“观察程序任务”页,然后单击“添加观察程序任务”按钮。 Select the Watcher tasks page and click on + Add a watcher task button.

  3. 输入“WatchMyFolder”作为名称。Enter "WatchMyFolder" as the name.

  4. 选择“配置观察程序”,然后选择“Watch-NewFile”runbook。 Select Configure watcher and select the Watch-NewFile runbook.

  5. 输入以下参数值:Enter the following values for the parameters:

    • 文件夹路径 - 混合辅助角色上在其中创建新文件的文件夹。FOLDERPATH - A folder on the hybrid worker where new files get created. d:\examplefilesd:\examplefiles
    • 扩展名 - 留空以便处理所有文件扩展名。EXTENSION - Leave blank to process all file extensions.
    • 递归 - 将此值保留为默认值。RECURSE - Leave this value as the default.
    • 运行设置 - 选择混合辅助角色。RUN SETTINGS - Pick the hybrid worker.
  6. 单击“确定”,然后选择返回到观察程序页。Click OK, and then Select to return to the watcher page.

  7. 选择“配置操作”,然后选择“Process-NewFile”runbook。 Select Configure action and select "Process-NewFile" runbook.

  8. 输入以下参数值:Enter the following values for parameters:

    • 事件数据 - 保留为空。EVENTDATA - Leave blank. 从观察程序 runbook 传入数据。Data is passed in from the watcher runbook.
    • 运行设置 - 当此 runbook 在自动化服务中运行时保留为 Azure。Run Settings - Leave as Azure as this runbook runs in the Automation service.
  9. 单击“确定”,然后选择返回到观察程序页。 Click OK, and then Select to return to the watcher page.

  10. 单击“确定”创建观察程序任务。 Click OK to create the watcher task.

从 UI 配置观察程序操作

触发观察程序Trigger a watcher

若要测试观察程序是否按预期工作,需要创建一个测试文件。To test the watcher is working as expected, you need to create a test file.

远程登录到混合辅助角色。Remote into the hybrid worker. 打开 PowerShell 并在文件夹中创建一个测试文件。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 the PROCESS AUTOMATION category.

  2. 选择观察程序任务“WatchMyFolder”。Select the watcher task "WatchMyFolder".

  3. 单击“流”下的“查看观察程序流”,查看观察程序是否发现新文件并启动了此操作 runbook。 Click on View watcher streams under Streams to see that the watcher found the new file and started the action runbook.

  4. 若要查看操作 runbook 作业,请单击“查看观察程序操作作业”。 To see the action runbook jobs, click on the View watcher action jobs. 可以选择每个作业并查看作业详细信息。Each job can be selected the view the details of the job.

    UI 中的观察程序操作作业

可以在下面的示例中看到当发现了新文件时的预期输出: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

在本教程中,你将了解:In this tutorial, you learned how to:

  • 导入观察程序 runbookImport a watcher runbook
  • 创建自动化变量Create an Automation variable
  • 创建操作 runbookCreate an action runbook
  • 创建观察程序任务Create a watcher task
  • 触发观察程序Trigger a watcher
  • 检查输出Inspect the output

单击以下链接可了解有关撰写 runbook 的详细信息。Follow this link to learn more about authoring your own runbook.