如何使用 PowerShell 在 Azure 存储表中获取一行?

How get a row in Azure Storage tables using PowerShell?

我正在尝试使用这些 PoserShell 命令获取 Azure 存储 table 中的所有行:

$saContext = (AzureRmStorageTable\Get-AzureRmStorageAccount -Name $storageAccount -ResourceGroupName $resourceGroup).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
Get-AzureStorageTableRowAll -table $table

但它会return这个错误:

Cannot find an overload for "ExecuteQuery" and the argument count: "1".
At C:\Program Files\WindowsPowerShell\Modules\AzureRmStorageTable.0.0.17\AzureRmStorageTableCoreHelper.psm1:305 char:6
+         $result = $table.CloudTable.ExecuteQuery($tableQuery)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

我什至也使用了这些命令行,但都会return同样的错误:

#Get-AzureStorageTableRowByColumnName -columnName "Average" -operator Equal -table $table -value 3228132966.4
#Get-AzureStorageTableTable -resourceGroup $resourceGroup -storageAccountName $storageAccount -tableName $tableName
#Get-AzureStorageTableRowAll -table $table | ft
#Get-AzureStorageTableRowByPartitionKey -table $table –partitionKey “I-Used-One-Of-My-Partition-Keys-From-Table” | ft

你知道我如何使用 PowerShell 在 Azure 存储 table 中获取一行吗?
事实上,我已经从 here.

安装了 AzureRmStorageTable

我发现这个错误是因为AzureRmStorageTable V1.0.0.17的一些问题。 我已经将它更新到 V1.0.0.20,现在可以使用了。 在 V1.0.0.20 的文档中,他们写道: 'Implemented some measures in order to avoid conflicts between different assembly versions, more specifically Microsoft.WindowsAzure.Storage.Dll.'

我不记得默认安装在Runbooks上的是哪个版本,反正你更新一下就可以了。

谢谢

这是 powershell 模块 Azure.Storage 和 AzureRm.Storage 中 dll 版本不同的结果。 AzureRm.Storage 的 Microsoft.WindowsAzure.Storage.dll 版本比 Azure.Storage 中的版本旧,并且缺少功能。一旦加载了两个程序集,就没有什么好方法可以告诉 powershell 使用哪个程序集了。

在此处查看问题: https://github.com/Azure/azure-powershell/issues/5030

我可以想到三个解决方法:

1) 如link所述,您可以使用New-Object创建指定程序集版本的powershell对象。例如:

$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $key
$sasToken = New-AzureStorageTableSASToken -Context $ctx -Permission a -Name $StorageTableName
$sasURI = $ctx.TableEndpoint + $StorageTableName + $sasToken
$cloudTable = New-Object -typename "Microsoft.WindowsAzure.Storage.Table.CloudTable, Microsoft.WindowsAzure.Storage, Version=8.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" -ArgumentList $sasURI

2) 在 powershell 中创建一个已编译的 class 来处理您的直接 .net 交互。 命令 Add-Type 创建您的 class,您可以在 -ReferencedAssemblies 参数中引用正确的 dll。这里有一个在 powershell 中使用 c# 的简单示例:http://activedirectoryfaq.com/2016/01/use-net-code-c-and-dlls-in-powershell/。这个选项最适合我。

3) 将较新版本的 Microsoft.WindowsAzure.Storage.dll 从 Azure.Storage 模块目录复制到 AzureRm.Storage 模块目录。这显然是一个脆弱的选项,但可能是最简单的快速修复。

这是如何使用新的 Az 命令获取所有行的答案,需要 AzTable 模块。

$storageAccountName = "name" 
$storageAccountKey = "key==" 
$context = New-AzStorageContext $storageAccountName -StorageAccountKey $storageAccountKey
$cloudTable = (Get-AzStorageTable –Name "table_name" –Context $context).CloudTable
Get-AzTableRow -Table $cloudTable

使用过滤器检索行:

[string]$filter = "Timestamp lt datetime'2019-05-27T13:58:04.0587693+02:00'"
Get-AzTableRow -Table $cloudTable -CustomFilter $filter

来自 Azure 的更多文档https://docs.microsoft.com/bs-latn-ba/azure/storage/tables/table-storage-how-to-use-powershell