Search code examples
azurepowershellazure-functionsazure-analysis-services

Azure powershell function app getting no parameter defined in the script or function for the input binding 'Timer'


I am creating an azure powershell function app that refreshes a data model in azure analysis services based on a timer trigger of 5 min every day. I have the following code in file called run.ps1:

# Import the SqlServer module
Import-Module SqlServer

param($Timer)

# Get the current universal time in the default string format
$currentUTCtime = (Get-Date).ToUniversalTime()

if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

# Define your service principal
$tenantId = "tenantId"
$clientId = "clientId"
$clientSecret = ConvertTo-SecureString $env:clientSecret -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($clientId, $clientSecret)

# Analysis Services details
$envUpper = $env:envUpper
$env = $envUpper.ToLower()
$analysisServicesServerPrefix = "myserverprefix"
$fullServerName = $analysisServicesServerPrefix + $env
$analysisServicesDatabase = "db"

try {
    # Authenticate with service principal
    Connect-AzAccount -ServicePrincipal -Credential $cred -Tenant $tenantId

    # Refresh the data model using Invoke-ProcessASDatabase
    Invoke-ProcessASDatabase -Server $fullServerName -DatabaseName $analysisServicesDatabase -RefreshType "Full"
} catch {
    # Write the error message and throw error
    Write-Error "Error occurred during data model refresh: $_"
    throw $_
}

# Write an information log with the current time
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"

I have the following code in function.json

{
  "bindings": [
    {
      "name": "Timer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

Don't understand why I am getting this error since I have defined the timer in the bindings.

Based on the docs, I have done the bindings correct: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=python-v2%2Cin-process%2Cnodejs-v4&pivots=programming-language-powershell

below is my requiremts.psd1

@{
    # Use version 11.1.0 of the Az module.
    'Az' = '11.1.0'

    # Use version 22.1.1 of the SqlServer module.
    'SqlServer' = '22.1.1'

}

folder structure can be seen in the image below (i have both run.ps1 and function.json in a subfolder called RefreshDataModel)

enter image description here

full error log

Executing 'Functions.RefreshDataModel' (Reason='Timer fired at 2024-01-24T10:05:00.0031544+00:00', Id=0a1accb8-a2f5-4949-a13c-74daa8f8b05b)
Information
2024-01-24 10:05:00.031
Result: Failure Exception: No parameter defined in the script or function for the input binding 'Timer'. Stack: at Microsoft.Azure.Functions.PowerShellWorker.AzFunctionInfo..ctor(RpcFunctionMetadata metadata) in /mnt/vss/_work/1/s/src/FunctionInfo.cs:line 134 at Microsoft.Azure.Functions.PowerShellWorker.FunctionLoader.LoadFunction(FunctionLoadRequest request) in /mnt/vss/_work/1/s/src/FunctionLoader.cs:line 52 at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessFunctionLoadRequest(StreamingMessage request) in /mnt/vss/_work/1/s/src/RequestProcessor.cs:line 245
Error
2024-01-24 10:05:00.031
Executed 'Functions.RefreshDataModel' (Failed, Id=0a1accb8-a2f5-4949-a13c-74daa8f8b05b, Duration=26ms)
Error
2024-01-24 10:05:00.039
Result: Failure Exception: No parameter defined in the script or function for the input binding 'Timer'. Stack: at Microsoft.Azure.Functions.PowerShellWorker.AzFunctionInfo..ctor(RpcFunctionMetadata metadata) in /mnt/vss/_work/1/s/src/FunctionInfo.cs:line 134 at Microsoft.Azure.Functions.PowerShellWorker.FunctionLoader.LoadFunction(FunctionLoadRequest request) in /mnt/vss/_work/1/s/src/FunctionLoader.cs:line 52 at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessFunctionLoadRequest(StreamingMessage request) in /mnt/vss/_work/1/s/src/RequestProcessor.cs:line 245
Error

Solution

  • The issue is that the param keyword is not at the topmost of your code (and it must be for scripts). The only statements that could go above it are using statements and attributes to decorate your script or function, like CmdletBinding or OutputType (both are irrelevant for an Azure Function). Also #Requires statements, irrelevant for Azure Functions too.

    # Import the SqlServer module
    Import-Module SqlServer
    
    param($Timer)
    

    Should be:

    param($Timer)
    
    # Import the SqlServer module
    Import-Module SqlServer