Skip to content

Instantly share code, notes, and snippets.

@daniel0x00
Last active December 27, 2020 09:26
Show Gist options
  • Save daniel0x00/c6f8920f424614c192233e0cb235eb00 to your computer and use it in GitHub Desktop.
Save daniel0x00/c6f8920f424614c192233e0cb235eb00 to your computer and use it in GitHub Desktop.
PowerShell Azure Function that takes an array with desired objects as child arrays as an input and outputs a single array object with child items only.
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Author: Daniel Ferreira (@daniel0x00)
# License: BSD 3-Clause
# Expected input POST Body schema:
# {
# "InputObject": ["<object>","<object>"], --> input array object.
# "ExpandProperty": "<string>",
# "MergeProperty": "<string>"
# }
## EXAMPLE:
# INPUT:
# {
# "ExpandProperty": "ContentData",
# "MergeProperty": "records",
# "InputObject": [
# {
# "ContentData": {
# "records": [
# {
# "time": "2020-12-26T12:12:58.7181726Z",
# "operationName": "MICROSOFT.COMPUTE/DISKS/DELETE",
# "category": "Administrative",
# "resultType": "Success",
# "resultSignature": "Succeeded.",
# "durationMs": "0",
# "level": "Information"
# }
# ]
# },
# "Properties": {
# "x-opt-sequence-number": 1662941,
# "x-opt-offset": "811766696840",
# "x-opt-enqueued-time": "2020-12-26T12:22:03.399Z"
# },
# "SystemProperties": {
# "EnqueuedTimeUtc": "2020-12-26T12:22:03.399Z",
# "Offset": "811766696840",
# "PartitionKey": null,
# "SequenceNumber": 1662941
# }
# },
# {
# "ContentData": {
# "records": [
# {
# "time": "2020-12-26T12:10:51.7568646Z",
# "category": "Administrative",
# "resultType": "Success",
# "resultSignature": "Succeeded.",
# "durationMs": "0",
# "level": "Information",
# }
# ]
# },
# "Properties": {
# "x-opt-sequence-number": 1662942,
# "x-opt-offset": "811766699280",
# "x-opt-enqueued-time": "2020-12-26T12:22:06.213Z"
# },
# "SystemProperties": {
# "EnqueuedTimeUtc": "2020-12-26T12:22:06.213Z",
# "Offset": "811766699280",
# "PartitionKey": null,
# "SequenceNumber": 1662942
# }
# }
# ]
# }
# OUTPUT:
# [
# {
# "operationName": "MICROSOFT.COMPUTE/DISKS/DELETE",
# "durationMs": "0",
# "resultType": "Success",
# "time": "2020-12-26T12:12:58.7181726Z",
# "level": "Information",
# "resultSignature": "Succeeded.",
# "category": "Administrative"
# },
# {
# "operationName": "MICROSOFT.COMPUTE/VIRTUALMACHINES/DELETE",
# "durationMs": "0",
# "resultType": "Success",
# "time": "2020-12-26T12:10:51.7568646Z",
# "level": "Information",
# "resultSignature": "Succeeded.",
# "category": "Administrative"
# }
# ]
# Return:
$StatusCode = [httpstatuscode]::OK
# Set output vars:
$Output = [string]::empty
try {
# Parse input:
$InputObject = $Request.Body.InputObject
$ExpandProperty = $Request.Body.ExpandProperty
$MergeProperty = $Request.Body.MergeProperty
# Const:
$JSONDepth = 10
## Output:
$Output = @(($InputObject | Select-Object -ExpandProperty $ExpandProperty).$MergeProperty) | ConvertTo-Json -Depth $JSONDepth -AsArray -Compress
}
catch {
Write-Verbose "Catch triggered: $_" -Verbose
$StatusCode = [httpstatuscode]::InternalServerError
}
finally {
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
ContentType = 'application/json'
StatusCode = $StatusCode
Body = $Output
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment