PowerShell Azure Functions lessons learned

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

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                                             
DebugPreference                SilentlyContinue                                 
Error                          {}                                               
ErrorActionPreference          Continue                                         
ErrorView                      NormalView                                       
ExecutionContext               System.Management.Automation.EngineIntrinsics    
false                          False                                            
FormatEnumerationLimit         4                                                
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                                                
OutputEncoding                 System.Text.ASCIIEncoding                        
outputFile                     D:\local\Temp\Functions\Binding\6528a25c-61cc-...
PID                            9936                                             
ProgressPreference             Continue                                         
PSBoundParameters              {}                                               
PSCulture                      en-US                                            
PSDefaultParameterValues       {}                                               
PSHOME                         D:\Windows\SysWOW64\WindowsPowerShell\v1.0       
PSSessionApplicationName       wsman                                            
PSSessionConfigurationName     http://schemas.microsoft.com/powershell/Micros...
PSSessionOption                System.Management.Automation.Remoting.PSSessio...
PSUICulture                    en-US                                            
PSVersionTable                 {PSVersion, WSManStackVersion, SerializationVe...
PWD                            D:\Windows\system32                              
req                            D:\local\Temp\Functions\Binding\6528a25c-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-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:\local\Temp\Functions\Binding\6528a25c-61cc-...
result                         {System.Management.Automation.PSVariable, Syst...
ShellId                        Microsoft.PowerShell                             
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).

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:\local\Temp\Functions\Binding\677636fe-4384-42d2-94a4-55d14101ac99\res

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

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


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


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



Comments (2)

  1. H Shakir says:

    can we create little script for customers to create VMs, VNets, Resource Groups in Azure so they run the script and just provide the info like IPs and names of the VMs or resource groups.

    1. Josh says:

      Yes you can create a script for customers to create vms, vnets, resource groups in Azure.
      The simplest way to knock out most of the powershell/json is to create the resources you have named manually and then when you click on them in the new portal (portal.azure.com) there is a section on the virtual machine titled “automation script”.

      That should have everything you need in it…late I know on the reply but just thought I would throw it out there.

Skip to main content