0

running linux powershell azure function with classes and it will not find types.

here's an example func script from the template with me adding a couple fields on a class

these fields show up as unknown types on my local dev environment and in the portal on azure

# POST method: $req
using namespace Microsoft.Azure.Commands.Profile.Models.Core
using namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions

class Test{
    hidden [PSAzureContext] $azureContext
    hidden [IStorageContext] $storageContext

    Test([PSAzureContext] $ctx, [IStorageContext] $storectx){
        $this.azureContext = $ctx
        $this.storageContext = $storectx
    }
}

$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name

# GET method: each querystring parameter is its own variable
if ($req_query_name) 
{
    $name = $req_query_name 
}

Out-File -Encoding Ascii -FilePath $res -inputObject "Hello $name"
Chris Hayes
  • 3,876
  • 7
  • 42
  • 72

1 Answers1

0

You can make use of below code in your Powershell run file, For IStorageContext to work, You can refer this answer by Reza Aghaei

My run.ps in Azure Function Http Trigger Powershell:-

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

Import-Module Az.Resources
Import-Module Az.Accounts



$AppId="xxxxxb838-6d26a31435cb"
$AppSecret="xxxxxZ3ZgxRt8313-CS0ifbLE"

$SecureSecret = $AppSecret | ConvertTo-SecureString -AsPlainText -Force

$Credential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $AppId,$SecureSecret

$TenantID="xxxx4ce4-99ed-af9038592395"

Connect-AzAccount -ServicePrincipal -Credential $Credential -Tenant $TenantID 

$AccountName = "siliconrgaaa6"
$ResourceGroupName="siliconrg"

$StorageAccount=Get-AzStorageAccount -Name $AccountName  -ResourceGroupName $ResourceGroupName
# $storageAccountBlob=Get-AzStorageBlob -Container "blob" -Context $storageContext  -Prefix "host"

Function SomeCmdlet {
  param(
    [parameter(Mandatory=$true)]
    [object]$storageContext
  )
  New-AzureStorageContainer -Name "blob" -Context $storageContext -Permission Off
}

$StorageAccount
$ResponseMessage="The kind of storage account is " + $StorageAccount.kind

Push-OutputBinding -Name response -Value ([HttpResponseContext]@{
    StatusCode = [System.Net.HttpStatusCode]::OK
    Body = $ResponseMessage })

Make sure you uncomment Az module in requirements.psd like below:-

enter image description here

Output:-

enter image description here

enter image description here

Http Triggered successfully:-

enter image description here

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11