powershell - 如何使用 HTTP 触发器 PowerShell Azure Function 读取 Azure 表存储?

标签 powershell azure azure-functions

行键将在查询字符串中传递。创建表存储的“连接字符串”的函数需要什么?

最佳答案

假设您的函数应用程序中已有一个名为 AzureWebJobsStorage 的应用程序设置,该设置具有到表存储的连接字符串,然后要在 PowerShell 脚本中检索该值,您将添加以下内容:

$connectionString = $env:AzureWebJobsStorage;

但是,如果您只需根据行键写入表存储,则可以利用 Azure Functions 中已支持的表存储绑定(bind)。

假设您的表存储中已创建一个名为 testtable 的表,这就是我们需要写入的表。然后,这是一个示例设置,它从 HTTP 触发器的查询字符串中读取行键并将条目写入表存储。

function.json:

{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "authLevel": "anonymous"
    },
    {
      "type": "table",
      "name": "outputTable",
      "tableName": "testtable",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

run.ps1:

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

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

Write-Output "Message entity: '$requestBody'"
$entity = [PSObject]@{
  PartitionKey = $requestBody.role  
  RowKey = $req_query_rowkey
  AccountId = $requestBody.id
}

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

在 Postman 中测试:

enter image description here

日志查看:

2017-07-04T17:21:17.095 Function started (Id=775a36ce-9d71-454c-887c-05f08cfdb877)
2017-07-04T17:21:17.314 Message entity: '@{name=Azure; role=admin; id=78910}'
2017-07-04T17:21:17.314 Function completed (Success, Id=775a36ce-9d71-454c-887c-05f08cfdb877, Duration=222ms)

Azure 存储资源管理器中的表条目 View :

enter image description here

关于powershell - 如何使用 HTTP 触发器 PowerShell Azure Function 读取 Azure 表存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44910406/

相关文章:

Powershell:在目标系统上安装模块

azure - 为什么 Azure 在我的 cspkg 中找不到 IISConfigurator.exe?

c# - 自托管 JobHost 时查找托管 Uri

python - Azure Functions for python、结构化日志记录到 Application Insights?

azure-functions - 一个实例 Azure Function 如何处理多个分区事件中心触发器

powershell - 将Get-Childitem过滤为CSV的内容

powershell - 如何使用 Sonar-Runner.bat 动态设置 ProjectVersion

sharepoint - Powershell 错误 "The term ' Get-SPWeb' 未被识别为 cmdlet、函数的名称...”

azure - DocumentDb 上的文档未被替换

ssl - 如何将SSL证书从azure导出到本地服务器?