【问题标题】:Azure Function - Powershell and Azure storage table (INSERT OR REPLACE)Azure 函数 - Powershell 和 Azure 存储表(插入或替换)
【发布时间】:2021-07-21 17:41:53
【问题描述】:

我通常使用 node 来编写我的函数,但这需要在 powershell 中。

我正在尝试在 Azure 存储表中写入数据,但如果数据存在,则不会将其替换为以下代码:

Push-OutputBinding -Name outputTable -Value @{
                    PartitionKey = $PartitionKey
                    RowKey = $RowKey
                    Description = $description
        }

我一直在到处寻找(文档和谷歌),但如果行存在,我找不到替换行内容的方法,基本上我想执行与节点中相同的替换插入。

感谢您的帮助!

【问题讨论】:

  • 希望这就是您要找的。这是一篇关于使用 Powershell 更新 Azure 存储表中的实体的文章:docs.microsoft.com/en-us/azure/storage/tables/…,不幸的是,您必须先找到该行,如果存在则更新它。
  • 谢谢,我会试试这个,让你知道
  • 您好 Mike,我可以使用它来添加或更新表行,但是在 foreach 循环中使用它时遇到问题。虽然我有一个写入主机,它在推送到表之前打印数据(显示正确的数据),但当我去检查表时,数据放错了位置,A 有 B 的数据,C 的 B 和 C 是空的,你知道为什么会这样吗?
  • 嗨,迈克,我已经解决了,谢谢您的建议!

标签: azure powershell azure-functions azure-storage


【解决方案1】:

根据您的需要,您可以使用Insert Or Merge 操作。如果表中不存在,该操作将更新现有实体或插入新实体。关于如何在 Azure 函数中使用 powershell 进行操作,请参考以下步骤

  1. 安装模块

请在 requirements.psd1 中添加一个 'Az' = '5.*' 密钥对

  1. 脚本
import-module Az.Storage

$e = New-Object Microsoft.Azure.Cosmos.Table.DynamicTableEntity("Jim","test")
$pro= New-Object Microsoft.Azure.Cosmos.Table.EntityProperty("test")
$pros = New-Object 'System.Collections.Generic.Dictionary[String,Microsoft.Azure.Cosmos.Table.EntityProperty]'
$pros.add("Description",$pro)
$e.Properties=$pros

$aaccountName="andyprivate"
$accountkey="h4pP1fe****U8CacyVMlTWAUA5A=="
$tableName="test"

$ctx =New-AzStorageContext -StorageAccountName $aaccountName  -StorageAccountKey $accountkey
$table=Get-AzStorageTable -Name $tableName   -Context $ctx
[Microsoft.Azure.Cosmos.Table.TableOperation]$tableOperation=[Microsoft.Azure.Cosmos.Table.TableOperation]::InsertOrMerge($e)
$table.CloudTable.Execute($tableOperation)

【讨论】:

  • 您好 Jim,谢谢您的建议,这也适用于 Azure 表存储吗?我阅读了很多 Azure Cosmo 参考资料,它可能仅适用于该产品?
  • @Ostage 它也适用于 Azure 表存储。
【解决方案2】:

使用了 Mike 在我的帖子评论中建议的 https://docs.microsoft.com/en-us/azure/storage/tables/table-storage-how-to-use-powershell#updating-entities

在 foreach 循环中工作,如果您使用正则表达式进行匹配,请小心。

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多