使用 Azure PowerShell 为 Azure Cosmos 表 API 表创建资源锁Create a resource lock for Azure Cosmos Table API table using Azure PowerShell

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本示例需要 Azure PowerShell Az 1.0 或更高版本。This sample requires Azure PowerShell Az 1.0 or later. 运行 Get-Module -ListAvailable Az,查看已安装哪些版本。Run Get-Module -ListAvailable Az to see which versions are installed. 如果需要安装,请参阅安装 Azure PowerShell 模块If you need to install, see Install Azure PowerShell module.

运行 Connect-AzAccount -Environment AzureChinaCloud 以登录到 Azure 中国。Run Connect-AzAccount -Environment AzureChinaCloud to sign in to Azure China.

重要

除非在启用了 disableKeyBasedMetadataWriteAccess 属性的情况下先锁定 Cosmos DB 帐户,否则资源锁对使用任何 Cosmos DB SDK、任何工具(通过帐户密钥连接)或 Azure 门户进行连接的用户所作的更改均无效。Resource locks do not work for changes made by users connecting using any Cosmos DB SDK, any tools that connect via account keys, or the Azure Portal unless the Cosmos DB account is first locked with the disableKeyBasedMetadataWriteAccess property enabled. 要详细了解如何启用此属性,请参阅防止 SDK 更改To learn more about how to enable this property see, Preventing changes from SDKs.

示例脚本Sample script

# References:
# Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# Az.Resources | https://docs.microsoft.com/powershell/module/az.resources
# --------------------------------------------------
# Purpose
# 
# --------------------------------------------------
# Variables
# ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup"
$accountName = "myaccount"
$databaseName = "myDatabase"
# *****
$apiVersion = "2020-04-01" # Cosmos DB RP API version
$lockLevel = "CanNotDelete" # CanNotDelete or ReadOnly

$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$resourceTypeDatabase = "$resourceTypeAccount/tables"

$resourceNameDatabase = "$accountName/$databaseName"
$lockNameDatabase = "$accountName-$databaseName-Lock"
# --------------------------------------------------

Write-Host "Create a $lockLevel lock on resource $resourceNameDatabase"
New-AzResourceLock `
    -ApiVersion $apiVersion `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceTypeDatabase `
    -ResourceName $resourceNameDatabase `
    -LockName $lockNameDatabase `
    -LockLevel $lockLevel `
    -Force

Write-Host "List all locks on  Cosmos DB account $accountName to confirm lock creation"
Get-AzResourceLock `
    -ApiVersion $apiVersion `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceTypeAccount `
    -ResourceName $accountName

Write-Host "Delete lock on resource $resourceNameDatabase"
Remove-AzResourceLock `
    -ApiVersion $apiVersion `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceTypeDatabase `
    -ResourceName $resourceNameDatabase `
    -LockName $lockNameDatabase `
    -Force

Write-Host "List all locks on Cosmos DB account $accountName to confirm lock removal"
Get-AzResourceLock `
    -ApiVersion $apiVersion `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceTypeAccount `
    -ResourceName $accountName

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组以及与其关联的所有资源。After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

CommandCommand 说明Notes
Azure 资源Azure Resource
New-AzResourceLockNew-AzResourceLock 创建资源锁。Creates a resource lock.
Get-AzResourceLockGet-AzResourceLock 获取资源锁,或列出资源锁。Gets a resource lock, or lists resource locks.
Remove-AzResourceLockRemove-AzResourceLock 删除资源锁。Removes a resource lock.

后续步骤Next steps

有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档For more information on Azure PowerShell, see Azure PowerShell documentation.

可以在 Azure Cosmos DB PowerShell 脚本中找到其他 Azure Cosmos DB PowerShell 脚本示例。Additional Azure Cosmos DB PowerShell script samples can be found in the Azure Cosmos DB PowerShell scripts.