使用 Azure PowerShell 來管理 Azure 資源

瞭解如何使用 Azure PowerShell 搭配 Azure Resource Manager 管理 Azure 資源。 如需管理資源群組,請參閱使用 Azure PowerShell 管理 Azure 資源群組

關於管理資源的其他文章:

將資源部署至現有的資源群組

您可以使用 Azure PowerShell 直接部署 Azure 資源,或是部署 Resource Manager 範本以建立 Azure 資源。

部署資源

下列指令碼會建立儲存體帳戶。

$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

部署範本

下列指令碼會部署快速入門範本,以建立儲存體帳戶。 如需詳細資訊,請參閱快速入門:使用 Visual Studio Code 建立 Azure Resource Manager 範本

$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/quickstarts/microsoft.storage/storage-account-create/azuredeploy.json"
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -Location $location

如需詳細資訊,請參閱使用 Resource Manager 範本與 Azure PowerShell 來部署資源

部署資源群組與資源

您可以建立資源群組,並將資源部署至群組。 如需詳細資訊,請參閱建立資源群組並部署資源

將資源部署至多個訂用帳戶或資源群組

一般而言,您要將範本中的所有資源部署至單一資源群組。 不過,在某些情況下,您要將一組資源部署在一起,但將它們放在不同的資源群組或訂用帳戶中。 如需詳細資訊,請參閱將 Azure 資源部署至多個訂用帳戶或資源群組

刪除資源

下列指令碼示範如何刪除儲存體帳戶。

$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 資源群組刪除

移動資源

下列指令碼示範如何將儲存體帳戶從某個資源群組移至另一個資源群組。

$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

如需詳細資訊,請參閱 將資源移動到新的資源群組或訂用帳戶

鎖定資源

鎖定可以防止組織中的其他使用者意外刪除或修改重要資源,例如 Azure 訂用帳戶、資源群組或資源。

下列指令碼會鎖定儲存體帳戶,讓帳戶無法遭到刪除。

$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 

下列指令碼會取得儲存體帳戶的所有鎖定:

$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

下列指令碼會刪除儲存體帳戶的鎖定:

$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 Resource Manager 來鎖定資源

標記資源

標記可協助您依邏輯組織資源群組與資源。 如需資訊,請參閱使用標籤組織 Azure 資源

管理對資源的存取

Azure 角色型存取控制 (Azure RBAC) 是在 Azure 中管理資源存取權的方式。 如需詳細資訊,請參閱使用 Azure PowerShell 新增或移除 Azure 角色指派

下一步