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

使用 Azure PowerShell 和 Resource Manager 管理资源Manage resources with Azure PowerShell and Resource Manager

本文介绍如何使用 Azure PowerShell 和 Azure Resource Manager 管理解决方案。In this article, you learn how to manage your solutions with Azure PowerShell and Azure Resource Manager. 如果不熟悉 Resource Manager,请参阅 Resource Manager 概述If you are not familiar with Resource Manager, see Resource Manager Overview. 本文重点介绍管理任务。This article focuses on management tasks. 将能够:You will:

  1. 创建资源组Create a resource group
  2. 将资源添加到资源组Add a resource to the resource group
  3. 向资源添加标记Add a tag to the resource
  4. 根据名称或标记值查询资源Query resources based on names or tag values
  5. 向资源应用和删除锁Apply and remove a lock on the resource
  6. 删除资源组Delete a resource group

本文不演示如何将 Resource Manager 模板部署到订阅。This article does not show how to deploy a Resource Manager template to your subscription. 有关详细信息,请参阅使用 Resource Manager 模板和 Azure PowerShell 部署资源For that information, see Deploy resources with Resource Manager templates and Azure PowerShell.

Azure PowerShell 入门Get started with Azure PowerShell

如果未安装 Azure PowerShell,请参阅如何安装和配置 Azure PowerShellIf you have not installed Azure PowerShell, see How to install and configure Azure PowerShell.

如果过去安装了 Azure PowerShell,但最近未更新它,请考虑安装最新版本。If you have installed Azure PowerShell in the past but have not updated it recently, consider installing the latest version. 可通过用于安装的方法更新版本。You can update the version through the same method you used to install it. 例如,如果使用 Web 平台安装程序,请再次启动它以查找更新。For example, if you used the Web Platform Installer, launch it again and look for an update.

若要检查 Azure 资源模块的版本,请使用以下 cmdlet:To check your version of the Azure Resources module, use the following cmdlet:

Get-Module -ListAvailable -Name AzureRm.Resources | Select Version

本文已针对版本 3.3.0 更新。This article was updated for version 3.3.0. 如果使用更旧的版本,体验可能与本文中所示步骤不完全相同。If you have an earlier version, your experience might not match the steps shown in this article. 有关此版本中 cmdlet 的文档,请参阅 AzureRM.Resources 模块For documentation about the cmdlets in this version, see AzureRM.Resources Module.

登录到 Azure 帐户Log in to your Azure account

处理解决方案之前,必须登录到帐户。Before working on your solution, you must log in to your account.

若要登录到 Azure 帐户,请使用 Login-AzureRmAccount cmdlet。To log in to your Azure account, use the Login-AzureRmAccount cmdlet.

Login-AzureRmAccount

该 cmdlet 会提示提供 Azure 帐户的登录凭据。The cmdlet prompts you for the login credentials for your Azure account. 登录后它会下载帐户设置,供 Azure PowerShell 使用。After logging in, it downloads your account settings so they are available to Azure PowerShell.

该 cmdlet 将返回有关帐户和用于任务的订阅的信息。The cmdlet returns information about your account and the subscription to use for the tasks.

Environment           : AzureCloud
Account               : example@contoso.com
TenantId              : {guid}
SubscriptionId        : {guid}
SubscriptionName      : Example Subscription One
CurrentStorageAccount :

如果有多个订阅,可切换到其他订阅。If you have more than one subscription, you can switch to a different subscription. 首先,请看帐户的所有订阅。First, let's see all the subscriptions for your account.

Get-AzureRmSubscription

它将返回已启用和已禁用的订阅。It returns enabled and disabled subscriptions.

SubscriptionName : Example Subscription One
SubscriptionId   : {guid}
TenantId         : {guid}
State            : Enabled

SubscriptionName : Example Subscription Two
SubscriptionId   : {guid}
TenantId         : {guid}
State            : Enabled

SubscriptionName : Example Subscription Three
SubscriptionId   : {guid}
TenantId         : {guid}
State            : Disabled

若要切换到其他订阅,请使用 Set-AzureRmContext cmdlet 提供订阅名称。To switch to a different subscription, provide the subscription name with the Set-AzureRmContext cmdlet.

Set-AzureRmContext -SubscriptionName "Example Subscription Two"

创建资源组Create a resource group

必须先创建将包含资源的资源组,才能向订阅部署任何资源。Before deploying any resources to your subscription, you must create a resource group that will contain the resources.

请使用 New-AzureRmResourceGroup cmdlet 创建资源组。To create a resource group, use the New-AzureRmResourceGroup cmdlet. 该命令使用 Name 参数指定资源组的名称,并使用 Location 参数指定其位置。The command uses the Name parameter to specify a name for the resource group and the Location parameter to specify its location.

New-AzureRmResourceGroup -Name TestRG1 -Location "South Central US"

输入格式如下:The output is in the following format:

ResourceGroupName : TestRG1
Location          : southcentralus
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/{guid}/resourceGroups/TestRG1

如果稍后需要检索资源组,请使用以下 cmdlet:If you need to retrieve the resource group later, use the following cmdlet:

Get-AzureRmResourceGroup -ResourceGroupName TestRG1

若要获取订阅中的所有资源组,请勿指定名称:To get all the resource groups in your subscription, do not specify a name:

Get-AzureRmResourceGroup

将资源添加到资源组Add resources to a resource group

要将资源添加到资源组中,可使用 New-AzureRmResource cmdlet 或特定于要创建的资源类型的 cmdlet(例如 New-AzureRmStorageAccount)。To add a resource to the resource group, you can use the New-AzureRmResource cmdlet or a cmdlet that is specific to the type of resource you are creating (like New-AzureRmStorageAccount). 使用特定于资源类型的 cmdlet 可能更轻松,因为它包含新资源组所需属性的参数。You might find it easier to use a cmdlet that is specific to a resource type because it includes parameters for the properties that are needed for the new resource. 要使用 New-AzureRmResource,必须了解将不会提示而设置所有属性。To use New-AzureRmResource, you must know all the properties to set without being prompted for them.

但是,通过 cmdlet 添加资源可能导致将来出现混乱,因为新的资源不存在于 Resource Manager 模板中。However, adding a resource through cmdlets might cause future confusion because the new resource does not exist in a Resource Manager template. Microsoft 建议在 Resource Manager 模板中定义 Azure 解决方案的基础结构。Microsoft recommends defining the infrastructure for your Azure solution in a Resource Manager template. 通过模板,可以可靠地重复部署解决方案。Templates enable you to reliably and repeatedly deploy your solution. 本文使用 PowerShell cmdlet 创建存储帐户,但稍后从资源组生成模板。For this article, you create a storage account with a PowerShell cmdlet, but later you generate a template from your resource group.

以下 cmdlet 可创建存储帐户。The following cmdlet creates a storage account. 请勿使用示例所示的名称,而是为存储帐户提供唯一名称。Instead of using the name shown in the example, provide a unique name for the storage account. 此名称必须为 3 到 24 个字符,只能使用数字和小写字母。The name must be between 3 and 24 characters in length, and use only numbers and lower-case letters. 如果使用示例所示名称,将收到错误,因为该名称被使用。If you use the name shown in the example, you receive an error because that name is already in use.

New-AzureRmStorageAccount -ResourceGroupName TestRG1 -AccountName mystoragename -Type "Standard_LRS" -Location "South Central US"

如果稍后需要检索此资源组,请使用以下 cmdlet:If you need to retrieve this resource later, use the following cmdlet:

Get-AzureRmResource -ResourceName mystoragename -ResourceGroupName TestRG1

添加标记Add a tag

标记可用于根据属性组织资源。Tags enable you to organize your resources according to different properties. 例如,可能有不同资源组中的多项资源属于同一部门。For example, you may have several resources in different resource groups that belong to the same department. 可对这些资源应用部门标签和值,将其标记为属于同一类别。You can apply a department tag and value to those resources to mark them as belonging to the same category. 也可标记资源是用于生产环境还是测试环境。Or, you can mark whether a resource is used in a production or test environment. 在本文中,只对一项资源应用标记,但在环境中最好向所有资源应用标记。In this article, you apply tags to only one resource, but in your environment it most likely makes sense to apply tags to all your resources.

以下 cmdlet 将向存储帐户应用两个标记:The following cmdlet applies two tags to your storage account:

Set-AzureRmResource -Tag @{ Dept="IT"; Environment="Test" } -ResourceName mystoragename -ResourceGroupName TestRG1 -ResourceType Microsoft.Storage/storageAccounts

各个标记作为单个对象更新。Tags are updated as a single object. 若要向已包含标记的资源添加标记,请首先检索现有标记。To add a tag to a resource that already includes tags, first retrieve the existing tags. 将新标记添加到包含现有标记的对象,并将所有标记重新应用到资源。Add the new tag to the object that contains the existing tags, and reapply all the tags to the resource.

$tags = (Get-AzureRmResource -ResourceName mystoragename -ResourceGroupName TestRG1).Tags
$tags += @{Status="Approved"}
Set-AzureRmResource -Tag $tags -ResourceName mystoragename -ResourceGroupName TestRG1 -ResourceType Microsoft.Storage/storageAccounts

搜索资源Search for resources

使用 Find-AzureRmResource cmdlet 可按不同搜索条件检索资源。Use the Find-AzureRmResource cmdlet to retrieve resources for different search conditions.

  • 若要按名称获取资源,请提供 ResourceNameContains 参数:To get a resource by name, provide the ResourceNameContains parameter:

    Find-AzureRmResource -ResourceNameContains mystoragename
    
  • 若要获取资源组中的所有资源,请提供 ResourceGroupNameContains 参数:To get all the resources in a resource group, provide the ResourceGroupNameContains parameter:

    Find-AzureRmResource -ResourceGroupNameContains TestRG1
    
  • 若要获取具有某个标记名称和值的所有资源,请提供 TagNameTagValue 参数:To get all the resources with a tag name and value, provide the TagName and TagValue parameters:

    Find-AzureRmResource -TagName Dept -TagValue IT
    
  • 若要获取具有特定资源类型的所有资源,请提供 ResourceType 参数:To all the resources with a particular resource type, provide the ResourceType parameter:

    Find-AzureRmResource -ResourceType Microsoft.Storage/storageAccounts
    

获取资源 IDGet resource ID

很多命令采用资源 ID 作为参数。Many commands take a resource ID as a parameter. 若要获取资源 ID 并将其存储在变量中,请使用:To get the ID for a resource and store in a variable, use:

$webappID = (Get-AzureRmResource -ResourceGroupName exampleGroup -ResourceName exampleSite).ResourceId

锁定资源Lock a resource

需要确保不会意外删除或修改关键资源时,请对资源应用锁定。When you need to make sure a critical resource is not accidentally deleted or modified, apply a lock to the resource. 可指定 CanNotDeleteReadOnlyYou can specify either a CanNotDelete or ReadOnly.

若要创建或删除管理锁,必须有权执行 Microsoft.Authorization/*Microsoft.Authorization/locks/* 操作。To create or delete management locks, you must have access to Microsoft.Authorization/* or Microsoft.Authorization/locks/* actions. 在内置角色中,只有“所有者”和“用户访问管理员”有权执行这些操作。Of the built-in roles, only Owner and User Access Administrator are granted those actions.

若要应用锁定,请使用以下 cmdlet:To apply a lock, use the following cmdlet:

New-AzureRmResourceLock -LockLevel CanNotDelete -LockName LockStorage -ResourceName mystoragename -ResourceType Microsoft.Storage/storageAccounts -ResourceGroupName TestRG1

上例中,在删除锁之前,无法删除锁定的资源。The locked resource in the preceding example cannot be deleted until the lock is removed. 若要删除所,请使用:To remove a lock, use:

Remove-AzureRmResourceLock -LockName LockStorage -ResourceName mystoragename -ResourceType Microsoft.Storage/storageAccounts -ResourceGroupName TestRG1

有关设置锁的详细信息,请参阅使用 Azure Resource Manager 锁定资源For more information about setting locks, see Lock resources with Azure Resource Manager.

删除资源或资源组Remove resources or resource group

可以删除资源或资源组。You can remove a resource or resource group. 删除资源组时,还会删除该资源组中的所有资源。When you remove a resource group, you also remove all the resources within that resource group.

  • 若要从资源组中删除资源,请使用 Remove-AzureRmResource cmdlet。To delete a resource from the resource group, use the Remove-AzureRmResource cmdlet. 此 cmdlet 将删除该资源,但不会删除该资源组。This cmdlet deletes the resource, but does not delete the resource group.

    Remove-AzureRmResource -ResourceName mystoragename -ResourceType Microsoft.Storage/storageAccounts -ResourceGroupName TestRG1
    
  • 若要删除资源组及其所有资源,请使用 Remove-AzureRmResourceGroup cmdlet。To delete a resource group and all its resources, use the Remove-AzureRmResourceGroup cmdlet.

    Remove-AzureRmResourceGroup -Name TestRG1
    

使用这两个 cmdlet,都会要求确认要删除的资源或资源组。For both cmdlets, you are asked to confirm that you wish to remove the resource or resource group. 如果操作成功删除资源或资源组,将返回 TrueIf the operation successfully deletes the resource or resource group, it returns True.

使用 Azure 自动化运行 Resource Manager 脚本Run Resource Manager scripts with Azure Automation

本文演示如何通过 Azure PowerShell 对资源执行基本操作。This article shows you how to perform basic operations on your resources with Azure PowerShell. 如果使用更高级的管理方案,通常需要创建脚本,并按需或按计划重复使用该脚本。For more advanced management scenarios, you typically want to create a script, and reuse that script as needed or on a schedule. 通过 Azure 自动化,可自动执行用于管理 Azure 解决方案的常用脚本。Azure Automation provides a way for you to automate frequently used scripts that manage your Azure solutions.

以下主题演示如何使用 Azure 自动化、Resource Manager 和 PowerShell 来有效执行管理任务:The following topics show you how to use Azure Automation, Resource Manager, and PowerShell to effectively perform management tasks:

后续步骤Next steps