Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I'm trying to develop a function that would read data from an Azure Storage queue and write it to an Azure storage table. I can't seem to find anything relevant. I found the code to read the queue:

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

But there are no examples to write to the table, so I'm not sure how to do that.

like image 530
ilivetoserve Avatar asked Jan 23 '26 14:01

ilivetoserve


2 Answers

Recently I did the same function and you can find the examples here. You need the function 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

To control to which table to write data you can use function.json. For me row and partition keys were specified there:

{
  "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"
}

This is my function.json, but originally it had partition and row key values hardcoded into it. Right now I'm using powershell to generate those (copy-pasted from another answer in this thread):

PartitionKey = $requestBody.room  
RowKey = get-date -Format "yyyy-MM-dd H:m:s.ms"
like image 52
likmoon Avatar answered Jan 25 '26 12:01

likmoon


As detailed in the PowerShell storage documentation, in order to write entities to table storage you need to provide a unique PartitionKey and RowKey value. Because of this, unless you're managing RowKeys outside in whatever's calling your Function, I found a datetime stamp to be useful. With this in mind, an incoming json body like this:

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

feeds into your PowerShell function (both WebHook and Queue trigger examples provided):

# 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

This results in one new entity (can be thought of as a row in database terms); assuming you've configured an Azure Table Storage output object in the Function and called it outputTable.

like image 42
AndyHerb Avatar answered Jan 25 '26 12:01

AndyHerb