[英]Azure Function App Push-OutputBinding Adding Subscription Information to JSON Output using Powershell
I have written several Azure Functions over the past year in both powershell and C#.在过去的一年中,我在 powershell 和 C# 中编写了几个 Azure 函数。 I am currently writing an API that extracts rows from a Storage Account Table and returns that data in a JSON format.
我目前正在编写一个 API,它从存储帐户表中提取行并以 JSON 格式返回该数据。
The data pulls fine.数据拉得很好。 The data converts to JSON just fine.
数据转换为 JSON 就好了。 A JSON formatted response is displayed - which is fine - but the Push-OutputBinding shoves in additional data to my original JSON data - account information, environment information, subscription information, and tenant information.
显示了 JSON 格式的响应 - 这很好 - 但是 Push-OutputBinding 将附加数据推到我的原始 JSON 数据中 - 帐户信息、环境信息、订阅信息和租户信息。
I've tried a number of different strategies for getting past this.我已经尝试了许多不同的策略来克服这个问题。 I gave up on using C# to interact with the Tables because the whole Azure.Data.Tables and Cosmos tables packages are a hot mess with breaking changes and package conflicts and.Net 6 requirements for new functions apps.
我放弃了使用 C# 与表格进行交互,因为整个 Azure.Data.Tables 和 Cosmos 表格包是一团糟,其中包含重大更改和 ZEFE90A8E604A7C840E88D03A67F6B7 的新功能冲突和要求。 So please don't offer up a C# solution unless you have a working example with specific versions for packages, etc.
所以请不要提供 C# 解决方案,除非您有一个包含特定版本的软件包的工作示例等。
Here is the code: Note that I have verified that $certData and $certJson properly formatted JSON that contain only the data I want to return.这是代码:请注意,我已验证 $certData 和 $certJson 正确格式化了 JSON ,其中仅包含我要返回的数据。
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
# Interact with query parameters or the body of the request.
$filter = $Request.Query.Filter
if (-not $filter) {
$filter = "ALL"
}
$certData = GetCerts $filter | ConvertTo-Json
#$certJson = $('{ "CertData":"' + $certData + '" }')
$body = "${CertData}"
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
ContentType = "application/json"
Body = $body
})
When I call the httpTrigger function, the response looks like this:当我调用 httpTrigger function 时,响应如下所示:
{ "CertData":"[
{
"Name": "MySubscriptionName blah blah",
"Account": {
"Id": "my user id",
"Type": "User",
....
},
"Environment": {
"Name": "AzureCloud",
"Type": "Built-in",
...
},
"Subscription": {
"Id": "SubscriptionID",
"Name": "SubscriptionName",
....
},
"Tenant": {
"Id": "TenandID",
"TenantId": "TenantId",
"ExtendedProperties": "System.Collections.Generic.Dictionary`2[System.String,System.String]",
...
},
"TokenCache": null,
"VersionProfile": null,
"ExtendedProperties": {}
},
{
"AlertFlag": 1,
"CertID": "abc123",
"CertName": "A cert Name",
"CertType": "an assigned cert type",
"DaysToExpire": 666,
"Domain": "WWW.MYDOMAIN.COM",
"Expiration": "2033-10-04T21:31:03Z",
"PrimaryDomain": "WWW.MYDOMAIN.COM",
"ResourceGroup": "RANDOM-RESOURCES",
"ResourceName": "SOMERESOURCE",
"Status": "OK",
"Subscription": "MYSUBSCRIPTIONNAME",
"Thumbprint": "ABC123ABC123ABC123ABC123ABC123",
"PartitionKey": "PARKEY1",
"RowKey": "ID666",
"TableTimestamp": "2022-02-03T09:00:28.7516797-05:00",
"Etag": "W/\"datetime'2022-02-03T14%3A00%3A28.7516797Z'\""
},
...
Not only does the returned values add data I don't want exposed, it makes parsing the return data that I do want to get when I make API calls problematic.返回的值不仅添加了我不想暴露的数据,而且当我进行 API 调用时,解析我确实想要获得的返回数据也有问题。
How do I get rid of the data added by the Push-OutputBinding?如何摆脱 Push-OutputBinding 添加的数据?
Was able to resolve issue by modifying run.ps1 as follows:能够通过修改 run.ps1 来解决问题,如下所示:
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
# Interact with query parameters or the body of the request.
$filter = $Request.Query.Filter
if (-not $filter) {
$filter = "ALL"
}
$certData = ( GetCerts $filter | Select-Object -Skip 1 )
#write-information $certData | Format-List
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $certData
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.