Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adbertram/63b25d37b1100309778ab08a95058b98 to your computer and use it in GitHub Desktop.
Save adbertram/63b25d37b1100309778ab08a95058b98 to your computer and use it in GitHub Desktop.
###################################################
# This is an associated snippet on learning Azure Functions by Jeff Brown at:
# https://adamtheatuomator.com/azure-functions-tutorial
###################################################
## The using statement imports the System.Net namespace, and param() defines the function's parameters.
using namespace System.Net
## Input bindings are passed in via param block.
## The *$Request* parameter contains the information passed to the function during the HTTP request. This information includes the request headers, request body, HTTP method, parameters, and URL of the request.
## The $TriggerMetadata parameter contains information about the triggered function. The sys property includes data like the date and time it is triggered, the HTTP method used to trigger it, and a unique GUID for the function's execution.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
# When viewing the console log during execution, the function code can output messages to the console. This output allows you to view the function progress in real-time. Add an output message stating the function is processing a request.
Write-Information 'LoggerFunction is processing a request.'
# Convert the incoming HTTP request body from JSON to a PowerShell object. The incoming HTTP request carries information about the request in the body. This information is accessible inside the function using the $Request parameter.
$requestBodyObject = $Request.Body | ConvertFrom-Json
# Create a new table entry object
# You need a PowerShell object to store information that will be saved to the table later. Create an object with four properties.
# The Severity and Message strings will be a part of the incoming HTTP request body when you trigger the function.
$tableObject = [PsCustomObject]@{
## Identifies the entry's table partition. Tables group entries by their partition key, and multiple entries can have the same partition key.
PartitionKey = 'partition1'
## Each entry in a partition requires a unique identifier. This example uses the New-Guid command to generate a unique GUID for each entry.
RowKey = (New-Guid).Guid
## The severity of the log message. Examples include INFO, WARN, ERROR, and so on.
Severity = $requestBodyObject.Severity
## The log message to store in the table.
Message = $requestBodyObject.Message
}
# Add object to table storage
# With the table entry built, you save the object to the table. Pushing data sends it to the output binding, in this case, the table in the storage account. You reference the table output binding created in Step 5 using the Name parameter. Pass the object data using the Value parameter.
Push-OutputBinding -Name outputLogTable -Value $tableObject
$body = "The entry was successfully logged to the table."
# Associate values to output bindings by calling 'Push-OutputBinding'.
# Finally, create an output message if the function successfully processes the HTTP request. You will see this message later in the PowerShell console when successfully invoking the function.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $body
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment