PowerShell REST 从 Azure 存储帐户删除 Table

PowerShell REST DELETE from Azure Storage Account Table

我正在努力使用 REST 方法通过 PowerShell 从 Azure 存储帐户中删除条目。我使用 SharedAccessSignature (SAS)(具有读取、写入和删除权限)进行身份验证以创建条目,但我无法将其用于删除条目。是否有人创建了一个 PowerShell 脚本来从 PowerShell 中删除 Azure 存储帐户表,并且可以向我发送有关如何执行此操作的代码片段?

我没有使用 PowerShell 模块,我使用的是“Invoke-WebRequest”CMDlet。我是 REST API 的新手,所以也许我只是没有正确的想法?对于条目创建,我使用 Invoke-WebRequest 调用中的 URI 将 SAS 令牌作为身份验证,但将“-Method POST”更改为“-Method DELETE”不起作用。

感谢您的帮助

要使用 REST 方法删除 table,如果有帮助,请使用以下示例查询:

  • 不要使用“Invoke-WebRequest”,而是使用 “Invoke-RestMethod”,如下所示
function DeleteTableEntity($TableName,$PartitionKey,$RowKey) {
$resource = "$tableName(PartitionKey='$PartitionKey',RowKey='$Rowkey')"
$table_url = "https://$storageAccount.table.core.windows.net/$resource"
$GMTTime = (Get-Date).ToUniversalTime().toString('R')
$stringToSign = "$GMTTime`n/$storageAccount/$resource"
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Convert]::FromBase64String($accesskey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign))
$signature = [Convert]::ToBase64String($signature)
$headers = @{
'x-ms-date' = $GMTTime
Authorization = "SharedKeyLite " + $storageAccount + ":" + $signature
Accept = "application/json;odata=minimalmetadata"
'If-Match' = "*"
}
$item = Invoke-RestMethod -Method DELETE -Uri $table_url -Headers $headers -ContentType application/http
}

详情请参考下方link:

Use Azure Table Storage via PowerShell and the Rest API - GCITS

  • 否则,您可以安装PowerShell模块,并使用如下脚本:
$resourceGroup = 'ResourceGroupName'
$storageAccountName = 'StorageAccountName'
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$ctx = $storageAccount.Context
$tables = (Get-AzStorageTable -Context $Ctx).name
ForEach ($table in $tables) {
Remove-AzStorageTable –Name $table –Context $ctx -Force
}

详情请参考下方link:

Delete all tables in Azure storage using powershell | Mike Says Meh.