question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature planning: first class PowerShell support

See original GitHub issue

This is a tracking item for First Class PowerShell support. We’ll be tracking the requirements and issues for first class PowerShell support via this item. Feel free to leave comments about features/design patterns.

Function format

For the Azure Function PowerShell format, we’ll keep the existing “scripting” model and limit the run file to the .ps1 format. A future feature will address .psm1 support with a proper “function” model.

This means we use the existing pattern of communicating data via files & environment variables. It also means that we don’t “cache” the environment that we run the script from. This will mean an inherent performance overhead, but this is likely acceptable for the scenarios where PowerShell scripting will be used. For more advanced scenarios, we’ll need to address those issues with psm1 support.

The scripting format, as is looks as so:

$in = Get-Content $Env:input

[Console]::WriteLine("Powershell script processed queue message '$in'")

$output = $Env:output
$json = $in | ConvertFrom-Json
$entity = [string]::Format('{{ "timestamp": "{0}", "status": 0, "title": "Powershell Table Entity for message {1}" }}', $(get-date -f MM-dd-yyyy_HH_mm_ss), $json.id)
$entity | Out-File -Encoding Ascii $output

Breaking this down, data coming in (via triggers and input bindings) is passed along via files which are communicated via environment variables, the names of which derive from the name property of the corresponding binding in the function.json. Data out works the same way. Any data being sent to output bindings is output to a local file specified via the environment variable corresponding to the name parameter in the function.json for the corresponding output bindings.

Data Type formats

All data is transferred via files. This means that it’s up to the user to parse the file to the right format.

Assuming the user knows which format the data in the file is in, all formats should be supportable.

  • String
    • File contents as is, assuming UTF-8 encoding
    • input example: [string]$str = Get-Contents $Env:input
    • output example: $str > $Env:output
  • Int
    • File contents, assuming file only contains a number
    • input example: [int]$int = Get-Contents $Env:input
    • output example: $int > $Env:output
  • Bool
    • File contents, assuming file only contains 0 or 1
    • input example: [bool]$bool = [int](Get-Contents $Env:input)
    • output example: $bool > $Env:output
  • Object/JSON
    • File contents, assuming its valid JSON
    • input exampe: $json = Get-Content $Env:input | ConvertFrom-Json
    • output example: $json | ConvertTo-Json > output.txt
  • Binary/Buffer
    • File contents
    • input example: [byte[]] $byte = Get-Content .$Env:input -Encoding Byte
    • output example $byte | Set-Content $Env:output -Encoding Byte
  • Stream (via file stream)
    • File contents
    • input example: $reader = [System.IO.File]::OpenText($Env:input)
    • output example $writer = [System.IO.StreamWriter] $Env:output
  • HTTP
    • TBD

Version & Package management

TBD

Testing/CI

TBD

Change log

  • 5/5 - Initial plan

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
Jaykulcommented, Oct 2, 2016

I was thinking more about azure doing the C# part, and letting people write PowerShell functions that would, to quote you:

mimic as close as possible the command-line and scripting workflows on the PowerShell.exe console.

As a general rule, when people are writing functions for the PowerShell console, it would be an anti-pattern to use file IO for parameters or output. Instead, functions should take parameters and output objects.

In my ideal world, your sample PowerShell template function would look more like this:

param($request)
Write-Verbose "PowerShell HTTP trigger function processed a request. RequestUri=$($req.RequestUri)"
<# ... do stuff #>
$name = $request.GetQueryNameValuePairs().Where({$_.Key -eq "Name" }).Value

return $req.CreateResponse("OK". "Hello $name");

But obviously that requires some code a little like what you wrote in your C# example – I don’t think people should have to write that themselves nor settle for having to serialize and deserialize through JSon on disk …

2reactions
tohlingcommented, Oct 2, 2016

@Jaykul, thank you for the clarification. I understand now that your expectation was that the user experience would be more akin to a PowerShell function. The term “PowerShell function” has a plural context at this point. We will keep this in mind and update our documentation to make the distinction clearer.

Unfortunately, the ideal workflow you suggested is not a supported scenario at this ‘Experimental’ stage. We appreciate this feedback and will add it as a consideration for our future planning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Desired State Configuration (DSC) Planning Update
This RC contains several community requested features and fixes including improved support for PSScriptAnalyzer, improvements to error handling, ...
Read more >
What is PowerShell and How to Use It: The Ultimate Tutorial
This comprehensive guide explains Windows PowerShell's key uses and features. Learn more about the flexible command-line interface and automation tool.
Read more >
PowerShell Master Class - PowerShell Fundamentals
The first module in the PowerShell Master Class. This is a multi-part class that should be viewed as part of its playlist, ...
Read more >
Feature Request: Add support for struct · Issue #12468
I found a workaround using C# code with Add-Type. Here is some PowerShell code that shows a limitation of using a class vs...
Read more >
How to Build a PowerShell GUI for your Scripts
First plan how you'd like the GUI to look and the elements you'd like to use. For this simple example, our GUI will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found