1

I have written several Azure Functions over the past year in both powershell and C#. I am currently writing an API that extracts rows from a Storage Account Table and returns that data in a JSON format.

The data pulls fine. The data converts to JSON just fine. 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.

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. So please don't offer up a C# solution unless you have a working example with specific versions for packages, etc.

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.

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:

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

How do I get rid of the data added by the Push-OutputBinding?

5
  • You shouldn't have to serialise anything to send it to Push-OutputBinding. I would hazard a guess that the object being returned from your GetCerts function is what is polluting your object and if you were to check the returned object with $certData | Format-List * that those properties were in fact valid for that object. You may have to use Select-Object to only return the properties that you want from GetCerts On a slightly different note are you using the table storage binding input as you can apply a filter within the function.json instead of making the call yourself. Commented Feb 3, 2022 at 15:22
  • You are indeed correct regards GetCerts polluting the data. I'll play with Select-Object to see if I can get the data I want. As for using table storage bindings in the function.json files... all the breaking changes and package conflicts involved in the push to Cosmos has made that a hot mess for both C# and PowerShell. Commented Feb 3, 2022 at 16:22
  • Of note, when I was debugging in VSCode, I wasn't seeing the extraneous data in my watched vars or when written out to file. This is why I assumed it had occured in the Push-Outputbinding step. Reviewing that data again, I see where it landed in the mix. Thanks! Commented Feb 3, 2022 at 16:26
  • Also, thanks for the info on not having to serialize the data! Commented Feb 3, 2022 at 16:38
  • I was able to get table storage working with filtering in the host.json but not at the same time as an optional route parameter so I ended up returning all values and filtering within the function which was fine for a small amount of entities but would quickly get awful if your table was large. Could share a code sample of how I got that working if you require. Commented Feb 3, 2022 at 17:14

1 Answer 1

1

Was able to resolve issue by modifying run.ps1 as follows:

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
    })
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.