更新 Azure PowerShell 模块Update Azure PowerShell modules

默认情况下,每个自动化帐户中都提供最常见的 PowerShell 模块。The most common PowerShell modules are provided by default in each Automation account. 请参阅默认模块See Default modules. 由于 Azure 团队会定期更新 Azure 模块,因此包含的 cmdlet 可能会发生更改。As the Azure team updates the Azure modules regularly, changes can occur with the included cmdlets. 这些更改(例如,重命名参数或完全弃用 cmdlet)可能会对 Runbook 产生负面影响。These changes, for example, renaming a parameter or deprecating a cmdlet entirely, can negatively affect your runbooks.

备注

不能删除全局模块,它们是自动化开箱提供的模块。You can't delete global modules, which are modules that Automation provides out of the box.

设置自动化帐户Set up an Automation account

为了避免影响 Runbook 及其自动化过程,请在继续操作之前进行测试和验证。To avoid impacting your runbooks and the processes they automate, be sure to test and validate as you make updates. 如果没有用于此目的专用自动化帐户,请考虑创建一个自动化帐户,以便可以在 Runbook 开发期间测试许多不同的方案。If you don't have a dedicated Automation account intended for this purpose, consider creating one so that you can test many different scenarios during the development of your runbooks. 该测试应该包括更新 PowerShell 模块等迭代更改。This testing should include iterative changes, such as updating the PowerShell modules.

请确保为自动化帐户创建了 Azure 运行方式帐户凭据Make sure that your Automation account has an Azure Run As account credential created.

如果在本地开发脚本,建议测试时在本地使用与自动化帐户中相同的模块版本,以确保收到相同的结果。If you develop your scripts locally, it's recommended to have the same module versions locally that you have in your Automation account when testing to ensure that you receive the same results. 在验证结果并且应用了所需的任何更改之后,则可将更改移到生产环境。After the results are validated and you've applied any changes required, you can move the changes to production.

备注

新自动化帐户可能不包含最新的模块。A new Automation account might not contain the latest modules.

获取用于更新的 RunbookObtain a runbook to use for updates

若要更新自动化帐户中的 Azure 模块,必须使用 Update-AutomationAzureModulesForAccount Runbook,该 Runbook 可用作开源。To update the Azure modules in your Automation account, you must use the Update-AutomationAzureModulesForAccount runbook, which is available as open source. 若要开始使用此 Runbook 更新 Azure 模块,请从 GitHub 存储库中下载。To start using this runbook to update your Azure modules, download it from the GitHub repository. 然后,可将其导入到自动化帐户,或作为脚本运行。You can then import it into your Automation account or run it as a script. 若要了解如何在自动化帐户中导入 Runbook,请参阅导入 RunbookTo learn how to import a runbook in your Automation account, see Import a runbook.

默认情况下,Update-AutomationAzureModulesForAccount Runbook 支持更新 Azure、AzureRM 和 Az 模块。The Update-AutomationAzureModulesForAccount runbook supports updating the Azure, AzureRM, and Az modules by default. 若要详细了解如何通过此 Runbook 更新 Az.Automation 模块,请查看更新 Azure 模块 Runbook 自述文件Review the Update Azure modules runbook README for more information on updating Az.Automation modules with this runbook. 在自动化帐户中使用 Az 模块时,还需要考虑其他重要因素。There are additional important factors that you need to take into account when using the Az modules in your Automation account. 若要了解更多信息,请参阅在 Azure 自动化中管理模块To learn more, see Manage modules in Azure Automation.

使用更新 Runbook 代码作为常规 PowerShell 脚本Use update runbook code as a regular PowerShell script

你可以使用 Runbook 代码作为常规 PowerShell 脚本而非 Runbook。You can use the runbook code as a regular PowerShell script instead of a runbook. 为此,请先使用 Connect-AzAccount cmdlet 登录到 Azure,然后将 -Login $false 传递给脚本。To do this, sign in to Azure using the Connect-AzAccount cmdlet first, then pass -Login $false to the script.

使用更新 runbookUse the update runbook

使用 AzEnvironment 参数将正确的环境传递给 runbook。Use the AzEnvironment parameter to pass the correct environment to the runbook. 可接受的值为 AzureChinaCloud。Acceptable value is AzureChinaCloud. 可通过使用 Get-AzEnvironment | select Name 来检索这些值。These values can be retrieved using Get-AzEnvironment | select Name.

使用更新 Runbook 更新特定模块版本Use the update runbook to update a specific module version

若要使用特定的 Azure PowerShell 模块版本而不是 PowerShell 库中的最新可用版本,请将这些版本传递给 Update-AutomationAzureModulesForAccount Runbook 的可选 ModuleVersionOverrides 参数。If you want to use a specific Azure PowerShell module version instead of the latest module available on the PowerShell Gallery, pass these versions to the optional ModuleVersionOverrides parameter of the Update-AutomationAzureModulesForAccount runbook. 有关示例,请参阅 Update-AutomationAzureModulesForAccount.ps1 Runbook。For examples, see the Update-AutomationAzureModulesForAccount.ps1 runbook. ModuleVersionOverrides 参数中未提及的 Azure PowerShell 模块会更新为 PowerShell 库中的最新模块版本。Azure PowerShell modules that aren't mentioned in the ModuleVersionOverrides parameter are updated with the latest module versions on the PowerShell Gallery. 如果没有向 ModuleVersionOverrides 参数传递任何内容,则所有模块都会更新为 PowerShell 库中的最新模块版本。If you pass nothing to the ModuleVersionOverrides parameter, all modules are updated with the latest module versions on the PowerShell Gallery.

后续步骤Next steps