Az Function App PowerShellPSHTML

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

In this blog post I will show a simple method to send email to the given address (Input form with a email input field and a submit button). Yes, you can think alternate for Office365 SMTP like send grid and mail jet services. But the ASKS is to send email with HTML body to given mail address. So, I opted for existing O365 to save time!

Output

Simple steps are as follows

  1. Add your O365 user name and password in key vaults.
  2. Add the key vaults secrets in your functions app settings.
  3. Just follow the documentation for Azure Functions—Key Vault integration
  4. Create two functions with HTTP triggers

iAbout

using namespace System.Net
param($Request, $TriggerMetadata)
$html = html -Content {
    head -Content {
        link -rel "stylesheet" -href "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" -integrity "sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" -crossorigin "anonymous"
        link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-all.min.css"
        link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro.min.css"
        link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-colors.min.css"
        link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-rtl.min.css"
        link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-icons.min.css"
        script -src "https://code.jquery.com/jquery-3.3.1.min.js"
        script -src "https://cdn.metroui.org.ua/v4/js/metro.min.js"
        script -src "https://code.jquery.com/jquery-3.2.1.slim.min.js" -integrity "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" -crossorigin "anonymous"
        script -src "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" -integrity "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" -crossorigin "anonymous"
        script -src "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" -integrity "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" -crossorigin "anonymous"
        title -Content "iContact"
    }
    body -Content {
        div -Class 'jumbotron' -Content {
            h1 -Class 'display-4' -Content "O365 Email..."
        }
        form -action '/api/iSendEmail' -method 'post' -enctype 'application/x-www-form-urlencoded' -target _blank -Content {
            div -Class 'form-group' -Content {
                input -type email -name email -Class 'form-control'
            }
            button -Attributes @{type = 'submit' } -Class 'btn btn-primary' -Content 'Send Email'
        }
    }
}
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        headers    = @{'content-type' = 'text/html' }
        StatusCode = [HttpStatusCode]::OK
        Body       = $html
    })

iSendEmail

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

$O365UserName = [System.Environment]::GetEnvironmentVariable('O365AdminUserName')
$O365Password = [System.Environment]::GetEnvironmentVariable('O365AdminPassword') | ConvertTo-SecureString -AsPlainText -Force
$Credential = [pscredential]::new($O365UserName, $O365Password)

$Body | ConvertTo-Html
$MailParams = @{
    From       = 'chendrayan@chensoffice365.onmicrosoft.com'
    To         = $($formdata.email)
    Subject    = 'Test mail from Az Function App...'
    Body       = @"
  Hi Chen V,
  
    This is a test mail from Azure Function app using the Office 365 SMTP server. 

  Regards,
  Chen V
"@
    SMTPServer = 'smtp.office365.com'
    Usessl     = $true
    Credential = $Credential
}

Send-MailMessage @MailParams 

Push-OutputBinding -name Response -Value ([HttpResponseContext]@{
        headers    = @{'content-type' = 'application/json' }
        StatusCode = [HttpStatusCode]::OK
        Body       = [pscustomobject]@{
            formdata = $formdata
            UserName = $O365UserName
            Message  = "Message Sent from O365"
        } | ConvertTo-Json
    })

Enjoy PowerShell – In my next blog we will discuss and demo the event driven automation using functions app with PowerShell.

2 thoughts on “Working with Azure Function App using PowerShell and PSHTML module – Part 3

  1. Hi Chen,
    Thanks for sharing this post. I have a question can we use Azure functions without internet on client side?
    I looking to have a UI to run my PowerShell scripts on IaaS project. User uses this UI to input the device information and that information gets saved on this server which later used by the PS scripts to pull the required information. So looking to develop a web application that will run on client’s server without internet.
    Is it possible using PSHTML and Azure Functions?

    Thanks
    Harikanth

    1. Yes, you can try Universal Dashboard for this need. PSHTML can be used in Azure Functions and this post talks about it. If you don’t need scalable solution – Polaris and PSHTML is best fit.

Leave a Reply

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