使用 Azure PowerShell 管理 Azure 资源管理器资源组Manage Azure Resource Manager resource groups by using Azure PowerShell

了解如何将 Azure PowerShell 与 Azure 资源管理器配合使用来管理 Azure 资源组。Learn how to use Azure PowerShell with Azure Resource Manager to manage your Azure resource groups. 若要管理 Azure 资源,请参阅使用 Azure PowerShell 管理 Azure 资源For managing Azure resources, see Manage Azure resources by using Azure PowerShell.

有关资源组管理的其他文章:Other articles about managing resource groups:

什么是资源组What is a resource group

资源组是用于保存 Azure 解决方案相关资源的容器。A resource group is a container that holds related resources for an Azure solution. 资源组可以包含解决方案的所有资源,也可以只包含以组的形式进行管理的资源。The resource group can include all the resources for the solution, or only those resources that you want to manage as a group. 根据对组织有利的原则,决定如何将资源分配到资源组。You decide how you want to allocate resources to resource groups based on what makes the most sense for your organization. 通常可将共享相同生命周期的资源添加到同一资源组,以便将其作为一个组轻松部署、更新和删除。Generally, add resources that share the same lifecycle to the same resource group so you can easily deploy, update, and delete them as a group.

” 资源组存储有关资源的元数据。The resource group stores metadata about the resources. 因此,当指定资源组的位置时,也就指定了元数据的存储位置。Therefore, when you specify a location for the resource group, you're specifying where that metadata is stored. 出于合规性原因,可能需要确保数据存储在某一特定区域。For compliance reasons, you may need to ensure that your data is stored in a particular region.

创建资源组Create resource groups

以下 PowerShell 脚本将创建一个资源组,然后显示该资源组。The following PowerShell script creates a resource group, and then shows the resource group.

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
$location = Read-Host -Prompt "Enter the location (i.e. chinaeast)"

New-AzResourceGroup -Name $resourceGroupName -Location $location

Get-AzResourceGroup -Name $resourceGroupName

列出资源组List resource groups

以下 PowerShell 脚本将列出订阅下的资源组。The following PowerShell script lists the resource groups under your subscription.

Get-AzResourceGroup

若要获取某个资源组,请执行以下操作:To get one resource group:

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"

Get-AzResourceGroup -Name $resourceGroupName

删除资源组Delete resource groups

以下 PowerShell 脚本删除某个资源组:The following PowerShell script deletes a resource group:

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"

Remove-AzResourceGroup -Name $resourceGroupName

若要详细了解 Azure 资源管理器如何管理资源的删除,请参阅 Azure 资源管理器资源组的删除For more information about how Azure Resource Manager orders the deletion of resources, see Azure Resource Manager resource group deletion.

将资源部署到现有的资源组Deploy resources to an existing resource group

请参阅将资源部署到现有的资源组See Deploy resources to an existing resource group.

若要验证资源组部署,请参阅 Test-AzResourceGroupDeploymentTo validate a resource group deployment, see Test-AzResourceGroupDeployment.

部署资源组和资源Deploy a resource group and resources

可以使用资源管理器模板创建一个资源组并将资源部署到该组。You can create a resource group and deploy resources to the group by using a Resource Manager template. 有关详细信息,请参阅创建资源组并部署资源For more information, see Create resource group and deploy resources.

部署失败时,重新部署Redeploy when deployment fails

此功能也称为“出错时回滚” 。This feature is also known as Rollback on error. 有关详细信息,请参阅在部署失败时重新部署For more information, see Redeploy when deployment fails.

移到另一个资源组或订阅Move to another resource group or subscription

可将组中的资源移到另一个资源组。You can move the resources in the group to another resource group. 有关详细信息,请参阅将资源移到新资源组或订阅For more information, see Move resources to new resource group or subscription.

锁定资源组Lock resource groups

锁定可以防止组织中的其他用户意外删除或修改关键资源,例如 Azure 订阅、资源组或资源。Locking prevents other users in your organization from accidentally deleting or modifying critical resources, such as Azure subscription, resource group, or resource.

以下脚本锁定了一个资源组,因此无法删除该资源组。The following script locks a resource group so the resource group can't be deleted.

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"

New-AzResourceLock -LockName LockGroup -LockLevel CanNotDelete -ResourceGroupName $resourceGroupName 

以下脚本获取资源组的所有锁:The following script gets all locks for a resource group:

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"

Get-AzResourceLock -ResourceGroupName $resourceGroupName 

有关详细信息,请参阅 使用 Azure 资源管理器锁定资源For more information, see Lock resources with Azure Resource Manager.

标记资源组Tag resource groups

可以将标记应用到资源组和资源,以按照逻辑组织资产。You can apply tags to resource groups and resources to logically organize your assets. 有关信息,请参阅使用标记组织 Azure 资源For information, see Using tags to organize your Azure resources.

将资源组导出到模板Export resource groups to templates

设置资源组后,可以查看资源组的资源管理器模板。After setting up your resource group, you can view a Resource Manager template for the resource group. 导出模板有两个好处:Exporting the template offers two benefits:

  • 由于模板包含整个基础结构,因此将来可以自动完成解决方案的部署。Automate future deployments of the solution because the template contains the complete infrastructure.
  • 通过查看代表解决方案的 JavaScript 对象表示法 (JSON),了解模板语法。Learn template syntax by looking at the JavaScript Object Notation (JSON) that represents your solution.

若要导出资源组中的所有资源,请使用 Export-AzResourceGroup cmdlet 并提供资源组名称。To export all resources in a resource group, use the Export-AzResourceGroup cmdlet and provide the resource group name.

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"

Export-AzResourceGroup -ResourceGroupName $resourceGroupName

它将模板另存为本地文件。It saves the template as a local file.

可以选择要导出的资源,而不是导出资源组中的所有资源。Instead of exporting all resources in the resource group, you can select which resources to export.

若要导出一个资源,请传递该资源 ID。To export one resource, pass that resource ID.

$resource = Get-AzResource `
  -ResourceGroupName <resource-group-name> `
  -ResourceName <resource-name> `
  -ResourceType <resource-type>
Export-AzResourceGroup `
  -ResourceGroupName <resource-group-name> `
  -Resource $resource.ResourceId

若要导出多个资源,请在数组中传递资源 ID。To export more than one resource, pass the resource IDs in an array.

Export-AzResourceGroup `
  -ResourceGroupName <resource-group-name> `
  -Resource @($resource1.ResourceId, $resource2.ResourceId)

导出模板时,可以指定是否在模板中使用参数。When exporting the template, you can specify whether parameters are used in the template. 默认情况下,包含资源名称的参数,但它们没有默认值。By default, parameters for resource names are included but they don't have a default value. 在部署过程中必须传递该参数值。You must pass that parameter value during deployment.

"parameters": {
  "serverfarms_demoHostPlan_name": {
    "defaultValue": null,
    "type": "String"
  },
  "sites_webSite3bwt23ktvdo36_name": {
    "defaultValue": null,
    "type": "String"
  }
}

在资源中,参数用于名称。In the resource, the parameter is used for the name.

"resources": [
  {
    "type": "Microsoft.Web/serverfarms",
    "apiVersion": "2016-09-01",
    "name": "[parameters('serverfarms_demoHostPlan_name')]",
    ...
  }
]

如果在导出模板时使用 -IncludeParameterDefaultValue 参数,则模板参数包括设置为当前值的默认值。If you use the -IncludeParameterDefaultValue parameter when exporting the template, the template parameter includes a default value that is set to the current value. 可以使用该默认值,也可以通过传入不同的值来覆盖默认值。You can either use that default value or overwrite the default value by passing in a different value.

"parameters": {
  "serverfarms_demoHostPlan_name": {
    "defaultValue": "demoHostPlan",
    "type": "String"
  },
  "sites_webSite3bwt23ktvdo36_name": {
    "defaultValue": "webSite3bwt23ktvdo36",
    "type": "String"
  }
}

如果在导出模板时使用 -SkipResourceNameParameterization 参数,则模板中不包括资源名称的参数。If you use the -SkipResourceNameParameterization parameter when exporting the template, parameters for resource names aren't included in the template. 而是,资源名称直接在资源上设置为其当前值。Instead, the resource name is set directly on the resource to its current value. 无法在部署过程中自定义该名称。You can't customize the name during deployment.

"resources": [
  {
    "type": "Microsoft.Web/serverfarms",
    "apiVersion": "2016-09-01",
    "name": "demoHostPlan",
    ...
  }
]

导出模板功能不支持导出 Azure 数据工厂资源。The export template feature doesn't support exporting Azure Data Factory resources. 若要了解如何导出数据工厂资源,请参阅在 Azure 数据工厂中复制或克隆数据工厂To learn about how you can export Data Factory resources, see Copy or clone a data factory in Azure Data Factory.

若要导出通过经典部署模型创建的资源,必须将其迁移到资源管理器部署模型To export resources created through classic deployment model, you must migrate them to the Resource Manager deployment model.

有关详细信息,请参阅将 Azure 门户中的单资源和多资源导出到模板For more information, see Single and multi-resource export to template in Azure portal.

管理对资源组的访问Manage access to resource groups

可以通过 Azure 基于角色的访问控制 (Azure RBAC) 管理对 Azure 中资源的访问权限。Azure role-based access control (Azure RBAC) is the way that you manage access to resources in Azure. 有关详细信息,请参阅使用 RBAC 和 Azure PowerShell 管理访问权限For more information, see Manage access using RBAC and Azure PowerShell.

后续步骤Next steps