使用 Azure PowerShell 來管理 Azure 資源Manage Azure resources by using Azure PowerShell

瞭解如何搭配Azure Resource Manager使用 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 資源,或部署 Resource Manager 範本來建立 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 Resource Manager 範本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

如需詳細資訊,請參閱使用 Resource Manager 範本與 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 Resource Manager 如何排序資源刪除的詳細資訊,請參閱Azure Resource Manager 資源群組刪除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

角色型存取控制 (RBAC) 是您對 Azure 中的資源存取進行管理的機制。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