Azure Function app with powershell - Connect-AzAccount error

Rafał Głębocki 6 Reputation points
2021-07-30T11:25:14.963+00:00

Hello,

I am running function app on consumption plan and have set system-assigned identity on it and added it as a contributor to subscription.

I want to do some basic operations on Azure resources. I've read that in consumption plan, host.json is not being executed before every run. So what I did, I have uploaded Az.Accounts module to 'modules' folder in my function directory. Then I have added Import-MModule and use Connect-AzAccount Identity.

Here is a code:

$DebugPreference='Continue'
Disable-AzureRmAlias
Import-Module "D:\home\site\wwwroot\MyFunction\modules\Az.Accounts\2.2.8\Az.Accounts.psd1"
Import-Module "D:\home\site\wwwroot\MyFunction\modules\Az.Resources\3.5.1\Az.Resources.psd1"
Import-Module "D:\home\site\wwwroot\MyFunction\modules\Az.LogicApp\1.5.0\Az.LogicApp.psd1"
Connect-AzAccount -Identity

Unfortunately I'm getting this error and have no idea how it is related.

2021-07-30T10:54:45.447 [Error] Connect-AzAccount : Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.at run.ps1: line 146+ Connect-AzAccount+ _________________+ CategoryInfo          : CloseError: (:) [Connect-AzAccount], FileNotFoundException+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand

Thanks for help!

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,409 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. AnuragSingh-MSFT 21,476 Reputation points
    2021-08-13T09:59:41.31+00:00

    Hi @Rafał Głębocki

    Apologies for late response. Here is some information that should help you.

    Azure Functions lets you leverage PowerShell gallery for managing dependencies. Therefore, in general you don't have to upload any PS module along with your function code. More details are available at Dependency Management in Azure Functions (PowerShell). This helps in easing the dependency management as well as to keep the version of modules updated.
    In this scenario, after enabling the "System assigned" identity, please ensure that requirements.psd1 file has the following line uncommented/added. This will automatically download required Az modules.
    'Az' = '6.*'
    Also, if you check the profile.ps1 file in the root folder of functions created through Azure Portal or template (VSCode, core tools), it has the code to runs the Connet-AzAccount -Identity when ManagedIdentity is enabled. Therefore, this need not be called again in the function code in that scenario.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.