Azure Function App - Az module found but could not be loaded

221 Views Asked by At

I have recently upgraded my Function apps Az module from 1.8.0 to 10.x.

Before requirements.psd1:

@{
    'Az' = '1.8.0'
}

After requirements.psd1:

@{
    'Az' = '10.*'
}

After upgrading, I am recieving this error when ever a function in the Az module is used: ERROR: The 'Connect-AzAccount' command was found in the module 'Az.Accounts', but the module could not be loaded. For more information, run 'Import-Module Az.Accounts'. Exception : Type : System.Management.Automation.CommandNotFoundException ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : The 'Connect-AzAccount' command was found in the module 'Az.Accounts', but the module could not be loaded. For more information, run 'Import-Module Az.Accounts'. HResult : -2146233087 TargetObject : Connect-AzAccount CategoryInfo : ObjectNotFound: (Connect-AzAccount:String) [], ParentContainsErrorRecordException FullyQualifiedErrorId : CouldNotAutoloadMatchingModule InvocationInfo : ScriptLineNumber : 15 OffsetInLine : 5 HistoryId : 1 ScriptName : C:\home\site\wwwroot\profile.ps1 Line : Connect-AzAccount -Identity PositionMessage : At C:\home\site\wwwroot\profile.ps1:15 char:5 . . .


  • I have tried restarting the function app and that has not resolved the issue.
  • I have tried updating my host.json to resolve the issue:

Before host.json:

{
    "version": "2.0",
    "managedDependency": {
      "enabled": true
    },
    "extensionBundle": {
      "id": "Microsoft.Azure.Functions.ExtensionBundle",
      "version": "[1.*, 2.0.0)"
    }
  }
  

After host.json:

{
    "version": "2.0",
    "managedDependency": {
        "Enabled": true
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.*, 4.0.0)"
    }
}

My function runtime version is 3.21.1.0.

I have rolled back to my previous deployment where Az is set to 1.8.0, and that allows Connect-AzAccount to function as expected, but I need to upgrade to utilize functionality in the newer versions.

I am aware including the entire Az module isn't recommended but I wanted to focus on getting a simple solution working first, then work on efficiency.

1

There are 1 best solutions below

0
RithwikBojja On BEST ANSWER

My host.json:

{
  "version": "2.0",
  "managedDependency": {
    "Enabled": true
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

requirements.psd1:

@{
    'Az' = '10.*'
}

enter image description here

PowerShell Runtime like below:

enter image description here

4.29.1.21919

With Above settings I am able to run and get output like below:

enter image description here

I would suggest you to create a new PowerShell Function App with latest version of PowerShell core.