Azure函数powershell,为什么azure表存储只插入一行?

时间:2018-01-04 12:30:22

标签: powershell azure-functions azure-table-storage azure-powershell

我尝试将日志插入到azure表存储中,但似乎无法追加行。以下是我的powershell脚本:

$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"

但是"测试日志1"从未插入过。 以下是天蓝色表格内容:

enter image description here

1 个答案:

答案 0 :(得分:2)

  

为什么azure表存储只插入一行?

如果您在Azure功能中拨打outputTable 多次最后一次可以作为outputTable的输出。

在您的情况下,您将输出调用outputtable两次,因此只需将最后一条记录插入到azure表中。

如果发送2个HTTP请求,那么您的azure表中将有2个“测试日志201”记录。

如果我们想只用一个HTTP请求插入多个记录,我们可以构建实体数组作为输出。

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

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

演示代码:

# 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"