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

簡介

此時此刻,您可能已經通過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 中使用A zure 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 Capabilities in Depth: The Azure Automation PowerShell Cmdlets