Az Function App PowerShellPowerShellPSHTML

Working with Azure Function App using PowerShell and PSHTML module – Part 1

Here is a quick demo of collecting user information and print on web browser as JSON. I know, this will not serve any purpose but give an idea of GET and POST in Azure Function App.

Yes, PSTHML works great with the Azure Function App. You may ask why to use function app with PowerShell when azure web app does the same or in better way? As an infrastructure developer I need to execute PowerShell scripts and for that I need web jobs (supplementary of web apps). So, UI is web app and for back end script executions web jobs needs to be created. With no time I can do all in one using PSHTML, Azure Function App and simple PowerShell scripts.

Requester Form (GET)

using namespace System.Net
param($Request, $TriggerMetadata)
$html = html {
    head {
        Title -Content "Requester Information"
        Link -href "https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" -rel "stylesheet"
        script -src "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" -type "text/javascript"
        script -src "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" -type "text/javascript"
        script -src "https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" -type "text/javascript"
    }

    body {
        div -Class "jumbotron text-center" -Content {
            h1 -Content "Requester Information"
            p -Content "A demo form for GET and POST"
        }

        Div -Class "container" -Content {
            form -Class "form-horizontal" -action "/api/iReceiver" -method "post" -target "_blank" -Attributes @{'autocomplete'='OFF'} -enctype 'application/x-www-form-urlencoded' -Content {
                Div -Class "form-group" -Content {
                    label -Class "control-label col-sm-2" -Attributes @{'for' = 'FirstName'} -Content "First Name"
                    Div -Class "col-sm-10" -Content {
                        input -type "text" -Class "form-control" -id "FirstName" -name "FirstName"
                    }
                }

                Div -Class "form-group" -Content {
                    label -Class "control-label col-sm-2" -Attributes @{'for' = 'LastName'} -Content "Last Name"
                    Div -Class "col-sm-10" -Content {
                        input -type "text" -Class "form-control" -id "LastName" -name "LastName"
                    }
                }

                Div -Class "form-group" -Content {
                    label -Class "control-label col-sm-2" -Attributes @{'for' = 'description'} -Content "Description:"
                    Div -Class "col-sm-10" -Content {
                        input -type "text" -Class "form-control" -id "description" -name "Description"
                    }
                }

                Div -Class "form-group" -Content {
                    Div -Class "col-sm-offset-2 col-sm-10" -Content {
                        button -Content "Submit" -Class "btn btn-default" -Attributes @{"type" = "submit"}
                    }
                }
            }
        }
    }
}

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        headers    = @{'content-type' = 'text/html'}    
        StatusCode = [HttpStatusCode]::OK
        Body       = $html
    })

Requester Form (POST)

using namespace System.Net;
using namespace System.Web;

param($Request, $TriggerMetadata)

$formdata = ([ordered]@{ })
$DecodedBody = [System.Web.HttpUtility]::UrlDecode($Request.Body)
($($DecodedBody) -split "&").ForEach( { $value = $_.split("="); $formdata.Add($value[0], $value[1]) })

Push-OutputBinding -name Response -Value ([HttpResponseContext]@{
        headers    = @{'content-type' = 'application/json' }
        StatusCode = [HttpStatusCode]::OK
        Body       = $formdata
    })

 

One thought on “Working with Azure Function App using PowerShell and PSHTML module – Part 1

Leave a Reply

Your e-mail address will not be published. Required fields are marked *