Examples/LI/run.ps1

# run.ps1 auto-generated by EasyAzureFunction module, https://github.com/iricigor/EasyAzureFunction, iricigor@gmail.com
'run.ps1 start'

# POST method: $req
$requestContent = Get-Content $req -Raw
try {
  $requestBody = $requestContent | ConvertFrom-Json
  $Failed = $false
  "Params (JSON): $($requestBody.PSObject.Properties.Name -join `",`")"
  $EzInvokeCommand = $requestBody.EzInvokeCommand
  $EzNumChars = $requestBody.EzNumChars
} catch {$Failed = $true}
if ($Failed) {
"Params (URLEncoded): $requestContent"
  $requestContent -split '&' | % {
    $v = $_ -split '='
    if ($v[1]) {Set-Variable -Name $v[0] -Value $v[1]}
  }
}

# prepare output, either default web-page or invoke command
cd $EXECUTION_CONTEXT_FUNCTIONDIRECTORY
if (!$EzInvokeCommand) {
  'show default web page'
  $Output = Get-Content .\index.html -Raw
} else {
  'invoke command'
  try {
    $ParamsHash = @{}
    if ($EzNumChars) {$ParamsHash.Add('NumChars',$EzNumChars)}
    "Params: $($ParamsHash.Keys -join `",`")"
    $Output = .\LoremIpsum.ps1 @ParamsHash | Out-String
    if ($Output) {$Color = 'white'}
    else {$Color = 'gray'; $Output = 'Command run successfully, but it returned no output'}
  } catch {
    $Output = $_
    $Color = 'red'
  }
  $Head = "<head><style>body {background-color: #012456; color: $Color;}</style><title>EasyAzureFunction - .\LoremIpsum.ps1 running example</title></head>"
  $Back = '<p><a href="javascript:history.back()" style="color:yellow;">Go Back</a></p>'
  $Output = $Head + '<pre>' + $Output + $Back + '</pre>'
  $Output = $Output -replace "`n",'</br>'
}

# parse and send back output
$Output2 = [PSCustomObject]@{Status = 200; Body = '';  Headers = @{}}
$Output2.Headers.Add('content-type','text/html')
$Output2.Body = $Output -replace '"',"'"

Out-File -Encoding utf8 -FilePath $res -inputObject ($Output2 | ConvertTo-JSON)