深度探究 Azure Automation 功能:Azure Automation PowerShell 命令

原文地址:https://azure.microsoft.com/blog/2014/08/20/azure-automation-capabilities-in-depth-the-azure-automation-powershell-cmdlets/

Joe Levy

Azure Automation项目经理

 

简介

此时此刻,您可能已经通过Azure门户试用过Azure Automation的出色功能:创作Runbook和创建资产、手动定期启动作业、导入集成模块以及查看作业输出。现在,您可以使用命令行更轻松地以编程方式完成所有这些操作。无论是不经意间使用Azure Automation还是希望为若干关键操作(如批量Runbook导入)编写脚本,Azure Automation 命令都可以为您实现。

Azure Automation 命令入门

Azure 提供了一个 PowerShell模块,该模块可以使用Microsoft Web Platform Installer安装,以便管理包括Azure Automation在内的各项Azure服务。

Azure Automation Capabilities in Depth: The Azure Automation PowerShell Cmdlets

Azure PowerShell模块目前提供20 个Azure Automation 命令,您可以使用这些命令编写脚本,来执行大量可从Azure Automation门户执行的操作。但这只是开始。到Azure Automation正式发布时,我们预计将会提供大约 40 个命令,从而通过PowerShell对Azure Automation实施全面控制。由于这些命令属于Azure PowerShell模块,我们还会提供 Azure PowerShell模块以便在Azure Automation中使用,无需在Runbook中执行任何额外操作即可使用这些命令,首次使用Azure Automation就能操作自如。

要查看Azure PowerShell模块提供的所有这些出色的命令,请在安装Azure模块的主机上打开PowerShell控制台,然后键入:

 PS C:\> Get-Command -Module Azure -Name *Automation*

这将返回所有与 Azure Automation 命令有关的信息:

Azure Automation Capabilities in Depth: The Azure Automation PowerShell Cmdlets

 

要获取如何使用某个特定 Automation 命令的详细信息,请键入:

 PS C:\> Get-Help Some-Command

使用具体的命令名称来替换上面的“Some-Command”,并在后面加上 “–Detailed” 或 “–Full”,即可获取有关该命令的详细信息。

Azure Automation Capabilities in Depth: The Azure Automation PowerShell Cmdlets

如果希望在 Web 浏览器(而非PowerShell控制台)中了解这些命令,还可以查看Azure Automation 命令参考

Azure Automation 命令的工作原理

与其他Azure 命令一样,您必须首先设置Azure订阅链接,以便对其执行Azure 命令。您可以使用本地证书存储中安装的管理证书或通过Azure凭据对Azure进行身份验证。最后,使用某种Set-AzureSubscription、Import-AzurePublishedSettingsFile、Add-AzureAccount和 Select-AzureSubscription组合设置Azure连接。您可以从此处查找有关如何使用Azure PowerShell 命令来连接Azure的详细信息。Azure连接设置完成后,不必指定任何凭据、端点等设置,即可使用Azure 命令。一切都以最初的连接设置为起点。但是,您需要通过AutomationAccountName参数提供要通过每一项操作访问的Azure Automation帐户的名称。

运行命令

以下是一个运行Azure Automation 命令的示例,并且展示了该命令提供的输出类型:

Azure Automation Capabilities in Depth: The Azure Automation PowerShell Cmdlets

使用命令的过程中,您将会发现一个问题:如果持续作用于同一Automation帐户,连续向每一个命令传递AutomationAccountName参数,可能会十分麻烦。幸运的是,用户不必每次写出(展开)即可轻松地将此参数传递至每一个Azure Automation 命令。展开允许您将一组参数作为一个HashTable进行定义,然后将这组参数传递给所有命令。从下图中,您可以看到,这会使向Azure Automation 命令添加Automation帐户名称参数和值的过程变得非常简便:

Azure Automation Capabilities in Depth: The Azure Automation PowerShell Cmdlets

在 Azure Automation中使用Azure Automation 命令

Azure PowerShell模块的另一个有趣的奥秘在于,它将作为开箱即用集成模块被纳入Azure Automation中,这意味着无需执行任何额外操作即可在Runbook中直接使用Automation 命令。例如,以下是一个Azure Automation Runbook,它通过异步方式以新作业的形式启动另一个Azure Automation Runbook,然后打印有关该作业的信息:

Azure Automation Capabilities in Depth: The Azure Automation PowerShell Cmdlets

 

要了解有关从Runbook启动其他Runbook的详细信息,请参阅Chris Sanders在此处发布的博客文章。如需其他Azure Automation Runbook示例(使用Azure Automation 命令),请查看Gary Keong发布的博客文章,该文的主题是集成Visual Studio Online 与 Azure Automation以实现Runbook来源控制。

小结

现在,希望大家对使用Azure Automation PowerShell 命令可以完成的所有精彩操作以及命令本身的工作原理有了更深入的了解。您不仅可以使用Azure Automation自动完成云和业务流程,甚至还可以使用这些命令自动操作Automation本身!Azure Automation团队热切盼望看到您编写的各种Runbook和脚本,以便充分利用Azure Automation 和 Azure PowerShell模块提供的功能。

还不是 Azure Automation 用户?注册预览版,然后查看入门指南