powershellazure-functionshelper

Azure function powershell script, how to implement multiple helper functions in the same Azure function?


I created an Azure Function. In this function I would like to implement some helper functions. I dont want to import modules, but would like to add the helper functions inside the function. How can I implement this?

For example

using namespace System.Net

function HelperFunctionCreateTab {param([string] $tabName, [string] $channelName)
    // some helper function logic
}

param($Request, $TriggerMetadata)
        
   Write-Output "START"
    
   // some main function logic

   HelperFunctionCreateTab -tabName "tabX" -channelName "channelY"

   // some main function logic
    
   Write-Output "START"

Solution

  • If you intent to implement the helper function in the same azure function then you need to add it after param block.

    param block needs to be at the top, after using block in your function or else you will get input binding error.

    I am using below code to implement the helper function.

    using namespace System.Net
    
    # Input bindings are passed in via param block.
    param($Request, $TriggerMetadata)
    
    function HelperFunctionCreateTab {
        param([string] $tabName, [string] $channelName)
        
        # Helper function logic
        Write-Output "Creating tab '$tabName' in channel '$channelName'"
    }
    
    Write-Output "START"
    
    # Call the helper function
    HelperFunctionCreateTab -tabName $Request.Query.tabName -channelName $Request.Query.channelName
    
    Write-Output "END"
    
    # Associate values to output bindings by calling 'Push-OutputBinding'.
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        StatusCode = [HttpStatusCode]::OK
        Body = "Function execution completed."
    })
    

    Output-

    enter image description here