azure function powershell 从存储队列中读取并写入存储 table

azure function powershell read from storage queue and write to storage table

所以我正在尝试开发一个函数,从 Azure 存储队列读取数据并将其写入 Azure 存储 table。我似乎找不到任何相关的东西。我找到了读取队列的代码:

$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"

但是没有示例可以写入 table,所以我不确定该怎么做。

最近我做了同样的功能,你可以找到例子here。您需要函数 QueueTrigger-PowerShell。 hth

$json = Get-Content $triggerInput | ConvertFrom-Json
Write-Output "PowerShell script processed queue message '$json'"

$title = "PowerShell Table Entity for message {0}" -f $json.id
$entity = [PSObject]@{
  Status = 0
  Title = $title
}

$entity | ConvertTo-Json | Out-File -Encoding UTF8 $outputTable

要控制向 table 写入数据,您可以使用 function.json。对我来说,行和分区键在那里指定:

{
  "type": "table",
  "name": "outputTable",
  "tableName": "pancakeTable",
  "connection": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
  "direction": "out",
  "partitionKey": "some value",
  "rowKey": "I don't remember what was here but it was some form of variable (guid) generated from the request by Azure"
}

这是我的 function.json,但最初它有硬编码的分区和行键值。现在我正在使用 powershell 生成这些(从该线程中的另一个答案复制粘贴):

PartitionKey = $requestBody.room  
RowKey = get-date -Format "yyyy-MM-dd H:m:s.ms"

PowerShell storage documentation 中所述,为了将实体写入 table 存储,您需要提供唯一的 PartitionKey 和 RowKey 值。因此,除非您在任何调用函数的外部管理 RowKey,否则我发现日期时间戳很有用。考虑到这一点,传入的 json 正文如下:

{
  "room": "Boardroom",
  "temp": "21.24"
}

输入您的 PowerShell 函数(提供了 WebHook 和队列触发器示例):

# WebHook example
$requestBody = Get-Content $req -Raw | ConvertFrom-Json

# Queue example
$requestBody = Get-Content $triggerInput | ConvertFrom-Json

Write-Output "PowerShell message body '$requestBody'"
$entity = [PSObject]@{
  PartitionKey = $requestBody.room  
  RowKey = get-date -Format "yyyy-MM-dd H:m:s.ms"
  Temp = $requestBody.temp
}

$entity | ConvertTo-Json | Out-File -Encoding UTF8 $outputTable

这会产生一个新实体(可以认为是数据库术语中的一行);假设你已经在函数中配置了一个 Azure Table 存储输出对象并将其命名为 outputTable.