1

I tried to insert log into azure table storage, but it seems it can not append rows. Here is my powershell script below:

$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name

# GET method: each querystring parameter is its own variable
if ($req_query_name) 
{
    $name = $req_query_name 
}
function LogToAzureTable( $log )
{
    $time = Get-Date -Format O
    $entity = [PSObject]@{
        PartitionKey = $EXECUTION_CONTEXT_INVOCATIONID
        RowKey = "PID($PID): ($time)"
        LogContent = $log
    }
    $entity | ConvertTo-Json | Out-File $outputTable
    $outputTable.done
}
Out-File -Encoding Ascii -FilePath $res -inputObject "Hello $name"

LogToAzureTable "test log 1"
LogToAzureTable "test log 201"

But the "test log 1" has never been inserted. Below is the azure table content:

enter image description here

1 Answer 1

2

Why azure table storage only has one row inserted?

If you call outputTable multiple times in the Azure function, just the last time could be as output to outputTable.

In your case you call the output to outputtable twice, so just the last record could be insert to azure table.

If send 2 HTTP requests then there will be 2 "test log 201" records in your azure table.

If we want to insert multiple records with just one HTTP request, we could construct an entity array as output.

  [PSObject []] $sampleArray = $entity,$entity2 

  $sampleArray |ConvertTo-Json| Out-File $outputTable

Demo code:

# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name

# GET method: each querystring parameter is its own variable
if ($req_query_name) 
{
    $name = $req_query_name 
}
function LogToAzureTable( $log )
{
    $time = Get-Date -Format O
    $entity = [PSObject]@{
        PartitionKey = $EXECUTION_CONTEXT_INVOCATIONID
        RowKey = "PID($PID): ($time)"
        LogContent = $log
    }
     $time = Get-Date -Format O
    $entity2 = [PSObject]@{
       PartitionKey = $EXECUTION_CONTEXT_INVOCATIONID
    RowKey = "PID($PID): ($time)2"
    LogContent = "$log-2"
    }

    [PSObject []] $sampleArray = $entity,$entity2 

     $sampleArray |ConvertTo-Json| Out-File $outputTable

    $outputTable.done
}

Out-File -Encoding Ascii -FilePath $res -inputObject "Hello $name"

LogToAzureTable "Hello $name"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.