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

使用 Azure PowerShell 管理 Azure 资源Manage Azure resources by using Azure PowerShell

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

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

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

你可以使用 Azure PowerShell 直接部署 Azure 资源,或部署资源管理器模板来创建 Azure 资源。You can deploy Azure resources directly by using Azure PowerShell, or deploy a Resource Manager template to create Azure resources.

部署资源Deploy a resource

以下脚本将创建一个存储帐户。The following script creates a storage account.

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$storageAccountName = Read-Host -Prompt "Enter the storage account name"

# Create the storage account.
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName `
  -Name $storageAccountName `
  -Location $location `
  -SkuName "Standard_LRS"

# Retrieve the context.
$ctx = $storageAccount.Context

部署模板Deploy a template

下面的脚本创建了部署快速入门模板,以创建存储帐户。The following script creates deploy a Quickstart template to create a storage account. 有关详细信息,请参阅快速入门:使用 Visual Studio Code 创建 Azure 资源管理器模板For more information, see Quickstart: Create Azure Resource Manager templates by using Visual Studio Code.

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json"
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -Location $location

有关详细信息,请参阅使用资源管理器模板和 Azure PowerShell 部署资源For more information, see Deploy resources with Resource Manager templates and Azure PowerShell.

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

你可以创建资源组,并将资源部署到组。You can create a resource group and deploy resources to the group. 有关详细信息,请参阅创建资源组并部署资源For more information, see Create resource group and deploy resources.

将资源部署到多个订阅或资源组Deploy resources to multiple subscriptions or resource groups

通常情况下,将模板中的所有资源部署到单个资源组。Typically, you deploy all the resources in your template to a single resource group. 不过,在某些情况下,你可能希望将一组资源部署在一起但将其放置在不同的资源组或订阅中。However, there are scenarios where you want to deploy a set of resources together but place them in different resource groups or subscriptions. 有关详细信息,请参阅将 Azure 资源部署到多个订阅或资源组For more information, see Deploy Azure resources to multiple subscriptions or resource groups.

删除资源Delete resources

以下脚本显示了如何删除存储帐户。The following script shows how to delete a storage account.

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

Remove-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageAccountName

有关资源管理器 Azure 如何对资源删除进行排序的详细信息,请参阅azure 资源管理器资源组删除For more information about how Azure Resource Manager orders the deletion of resources, see Azure Resource Manager resource group deletion.

移动资源Move resources

以下脚本显示了如何将存储帐户从一个资源组移到另一个资源组。The following script shows how to remove a storage account from one resource group to another resource group.

$srcResourceGroupName = Read-Host -Prompt "Enter the source Resource Group name"
$destResourceGroupName = Read-Host -Prompt "Enter the destination Resource Group name"
$storageAccountName = Read-Host -Prompt "Enter the storage account name"

$storageAccount = Get-AzResource -ResourceGroupName $srcResourceGroupName -ResourceName $storageAccountName
Move-AzResource -DestinationResourceGroupName $destResourceGroupName -ResourceId $storageAccount.ResourceId

有关详细信息,请参阅将资源移到新资源组或订阅For more information, see Move resources to new resource group or subscription.

锁定资源Lock resources

锁定可防止组织中的其他用户意外删除或修改关键资源,如 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 storage account so the account can't be deleted.

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

New-AzResourceLock -LockName LockStorage -LockLevel CanNotDelete -ResourceGroupName $resourceGroupName -ResourceName $storageAccountName -ResourceType Microsoft.Storage/storageAccounts 

以下脚本将获取存储帐户的所有锁:The following script gets all locks for a storage account:

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

Get-AzResourceLock -ResourceGroupName $resourceGroupName -ResourceName $storageAccountName -ResourceType Microsoft.Storage/storageAccounts

以下脚本将删除存储帐户的锁定:The following script deletes a lock of a storage account:

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

$lockId = (Get-AzResourceLock -ResourceGroupName $resourceGroupName -ResourceName $storageAccountName -ResourceType Microsoft.Storage/storageAccounts).LockId
Remove-AzResourceLock -LockId $lockId

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

标记资源Tag resources

标记有助于以逻辑方式组织资源组和资源。Tagging helps organizing your resource group and resources logically. 有关信息,请参阅使用标记来组织 Azure 资源For information, see Using tags to organize your Azure resources.

管理对资源的访问权限Manage access to resources

可以在 Azure 中通过基于角色的访问控制 (RBAC) 这种方式管理对资源的访问。Role-based access control (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