Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

PowerShell Azure Functions lessons learned

$
0
0

Lately I’m playing with PowerShell Azure Functions and I learned there are some things I needed to learn and that’s why I want to share some more info about this topic.

What are Azure Functions?

Azure Functions is an event driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in virtually any Azure or 3rd party service as well as on-premises systems. Azure Functions allows developers to take action by connecting to data sources or messaging solutions, thus making it easy to process and react to events. Azure Functions scale based on demand and you pay only for the resources you consume. (from Github)

On the Github page about Azure Functions you can find all the info to get started.

PowerShell Azure Functions

Azure Functions enables you to create scheduled or triggered units of code implemented in various programming languages. PowerShell is one of those programming languages.

A good starting point is a blog post from David O’Brien Azure Functions – PowerShell.

If you look at an example from David you see a special variable being used ‘$res’. At first it was not clear to me where this variable was being defined. To find out more I started to create a Function using the Get-Variable cmdlet.

Use the following PowerShell script in your Function to retrieve the automatic variables available in Azure PowerShell functions.

[sourcecode language='powershell'  padlinenumbers='true']
write-Output 'Getting variables'
$result = Get-Variable | out-string
[/sourcecode]

If you run this in your Azure PowerShell function you will see the following in your logs section.

[sourcecode language='powershell' ]
2017-01-29T16:01:51.538 Function started (Id=6528a25c-61cc-4d16-90ad-4869e47dc599)
2017-01-29T16:01:51.867 Getting variables
2017-01-29T16:01:51.898 Name                           Value
----                           -----
$
?                              True
^
args                           {}
ConfirmPreference              High
ConsoleFileName
DebugPreference                SilentlyContinue
Error                          {}
ErrorActionPreference          Continue
ErrorView                      NormalView
ExecutionContext               System.Management.Automation.EngineIntrinsics
false                          False
FormatEnumerationLimit         4
HOME
Host                           System.Management.Automation.Internal.Host.Int...
input                          System.Collections.ArrayList+ArrayListEnumerat...
InvocationId                   6528a25c-61cc-4d16-90ad-4869e47dc599
MaximumAliasCount              4096
MaximumDriveCount              4096
MaximumErrorCount              256
MaximumFunctionCount           4096
MaximumHistoryCount            4096
MaximumVariableCount           4096
MyInvocation                   System.Management.Automation.InvocationInfo
NestedPromptLevel              0
null
OutputEncoding                 System.Text.ASCIIEncoding
outputFile                     D:localTempFunctionsBinding6528a25c-61cc-...
PID                            9936
ProgressPreference             Continue
PSBoundParameters              {}
PSCommandPath
PSCulture                      en-US
PSDefaultParameterValues       {}
PSEmailServer
PSHOME                         D:WindowsSysWOW64WindowsPowerShellv1.0
PSScriptRoot
PSSessionApplicationName       wsman
PSSessionConfigurationName     http://schemas.microsoft.com/powershell/Micros...
PSSessionOption                System.Management.Automation.Remoting.PSSessio...
PSUICulture                    en-US
PSVersionTable                 {PSVersion, WSManStackVersion, SerializationVe...
PWD                            D:Windowssystem32
req                            D:localTempFunctionsBinding6528a25c-61cc-...
REQ_HEADERS_ACCEPT             application/json, */*
REQ_HEADERS_ACCEPT-ENCODING    gzip, deflate, peerdist
REQ_HEADERS_ACCEPT-LANGUAGE    en-US, en-GB; q=0.8, en; q=0.6, nl-NL; q=0.4, ...
REQ_HEADERS_CONNECTION         Keep-Alive
REQ_HEADERS_DISGUISED-HOST     stsfunctionappdemo.azurewebsites.net
REQ_HEADERS_HOST               stsfunctionappdemo.azurewebsites.net
REQ_HEADERS_MAX-FORWARDS       10
REQ_HEADERS_ORIGIN             https://functions.azure.com
REQ_HEADERS_REFERER            https://functions.azure.com/?trustedAuthority=...
REQ_HEADERS_USER-AGENT         Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl...
REQ_HEADERS_WAS-DEFAULT-HOS... stsfunctionappdemo.azurewebsites.net
REQ_HEADERS_X-ARR-LOG-ID       b61667f6-86d8-4bd1-a67c-bc0821ee2170
REQ_HEADERS_X-ARR-SSL          2048|256|C=US, S=Washington, L=Redmond, O=Micr...
REQ_HEADERS_X-FORWARDED-FOR    217.122.212.62:4004
REQ_HEADERS_X-FUNCTIONS-KEY    EOm0H6fRai398YoJRGKkjzAZ7SV2E/zgnOjTaCOVs55W8h...
REQ_HEADERS_X-LIVEUPGRADE      1
REQ_HEADERS_X-ORIGINAL-URL     /api/HttpTriggerPowerShellDemo?code=fYnNjQpSyo...
REQ_HEADERS_X-P2P-PEERDIST     Version=1.1
REQ_HEADERS_X-P2P-PEERDISTEX   MinContentInformation=1.0, MaxContentInformati...
REQ_HEADERS_X-SITE-DEPLOYME... stsfunctionappdemo
REQ_METHOD                     GET
REQ_QUERY_CODE                 fYnNjQpSyoUtXrR3fJ/vXn/L252RcTrzaeVFGP5vsMG6aa...
res                            D:localTempFunctionsBinding6528a25c-61cc-...
result                         {System.Management.Automation.PSVariable, Syst...
ShellId                        Microsoft.PowerShell
StackTrace
true                           True
VerbosePreference              SilentlyContinue
WarningPreference              Continue
WhatIfPreference               False
2017-01-29T16:01:52.976 Function completed (Success, Id=6528a25c-61cc-4d16-90ad-4869e47dc599)
2017-01-29T16:03:40  No new trace in the past 1 min(s).
[/sourcecode]

An interesting variable is “res” if we look at the value of this res variable we see that the value in my Azure Function is D:localTempFunctionsBinding677636fe-4384-42d2-94a4-55d14101ac99res

But I was still wondering where this variable was configured because in the examples I often saw the variable being used to output the result.

Example where $res variable is being used:

[sourcecode language='powershell' ]
$Result = $psversiontable | ConvertTo-Json
Out-File -encoding Ascii -FilePath $res -inputObject $Result
[/sourcecode]

It turned out this variable is being configured in the integration configuration section of the Azure Function.

image

So in above example the output result being show when the Azure Http Trigger PowerShell Function is being called stores the output of the Result variable in a file defined in the res variable.  Hope this clarifies the $res variable seen in many examples on the internet about Azure PowerShell functions.

If you are also interested in the available environment variables within Azure PowerShell functions you can use the following PowerShell script in your Azure Function:

[sourcecode language='powershell' ]
write-Output 'Getting environment variables'
$result = ls env: | out-string
write-output $result
[/sourcecode]

image

I hope you have learned some new things about Azure PowerShell functions and you are interested to get started.

 

References:


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>