1

Got a weird issue publishing a PowerShell Function to a Function App. The Function App uses Windows consumption with a Y1 App Service Plan.

The Function uses 5 different Az Modules that exist in the Modules Directory as part of the Package:

  • Az.Accounts
  • Az.Resources
  • Az.Compute
  • Az.Sql
  • Az.SqlVirtualMachine

The File structure looks like this:

wwwroot/
| - .funcignore
| - ahubReporter
| | - function.json
| | - readme.md
| | - run.ps1
| - host.json
| - Modules
| | - Az.Accounts
| | - Az.Compute
| | - Az.Resources
| | - Az.Sql
| | - Az.SqlVirtualMachine
| - profile.ps1
| - requirements.psd1

NOTE: managedDependency is not enabled and the Az Modules are not download automatically. The Modules are included in the source under the Modules directory and published with the function

If the Function is published manually through vscode using the Azure Function Extension, the function works and runs perfectly.

However, If I publish the Function by either the Azure DevOps Task AzureFunctionApp@2 or Azure PowerShell Task AzurePowerShell@5 using the command Publish-AzWebApp, the function is published, but does not run and I get the following error:

The 'Get-AzLocation' command was found in the module 'Az.Resources', but the module could not be loaded. For more information, run 'Import-Module Az.Resources'.

The Azure DevOps Pipeline is configured to use a Windows Agent.

The Azure DevOps Task AzureFunctionApp@2 is:

- task: AzureFunctionApp@2
  displayName: 'Publish Function | package'
  inputs:
    azureSubscription: ${{ parameters.customerServiceConnection }}
    appType: functionApp
    appName: $(functionName)
    package: $(System.ArtifactsDirectory)/**/*.zip
    deploymentMethod: 'runFromPackage' # <--- I've tried auto as well

The Azure DevOps Task AzurePowerShell@5 is:

- task: AzurePowerShell@5
  displayName: 'Publish Function | azpwsh'
  inputs:
    azureSubscription: ${{ parameters.customerServiceConnection }}
    ScriptType: 'InlineScript'
    Inline: 'Publish-AzWebapp -ResourceGroupName $(functionAppRg) -Name $(functionName) -ArchivePath $(System.ArtifactsDirectory)/build$(Build.BuildId).zip -Force'
    azurePowerShellVersion: 'LatestVersion'
     pwsh: true

Thanks!

3
  • Did you run : Import-Module Az.Resources Commented Jun 9, 2023 at 9:36
  • yes, doesn't work. The Module is already imported, but for some reason when the function is published via vscode - it's fine, but not when publish via the Azure DevOps task or PowerShell Publish-AzWebApp Commented Jun 11, 2023 at 3:49
  • Try troubleshooting : learn.microsoft.com/en-us/azure/devops/pipelines/… Commented Jun 11, 2023 at 7:49

3 Answers 3

1

I looked into this a little as the error messages suggests that the PSModulePath is not configured correctly as it can locate the function/cmdlet and the module but it's not able to load it.

It is my understanding that the Modules folder in the root of the project is appended to the PSModulePath when the worker is initialised.

I quickly built a project in a devcontainer to do some tests, disabled managed dependencies as you advised.

Ran Save-Module to save the Az.Resources module and added it to a Modules folder within the project.

When running the project you can see the logs in a bit better detail than you get from AzDo or the portal and there are a large amount of errors coming from the internal commands within Az.Resources & Az.Accounts there are also internal modules Az.Authorisation & Az.MsGraph which show in the logs with Newtonsoft collision errors.

 Message        : The 'Get-AzLocation' command was found in the module 'Az.Resources', but the module could not be loaded. For more information, run 'Import-Module Az.Resources'.
[2023-06-09T10:22:21.599Z]     Data           : System.Collections.ListDictionaryInternal
[2023-06-09T10:22:21.599Z]     InnerException : 
[2023-06-09T10:22:21.599Z]         Type           : System.Management.Automation.CmdletInvocationException
[2023-06-09T10:22:21.599Z]         ErrorRecord    : 
[2023-06-09T10:22:21.599Z]             Exception             : 
[2023-06-09T10:22:21.599Z]                 Type       : System.IO.FileLoadException
[2023-06-09T10:22:21.600Z]                 Message    : Assembly with same name is already loaded
[2023-06-09T10:22:21.600Z]                 TargetSite : 
[2023-06-09T10:22:21.600Z]                     Name          : Bind_LoadAssemblies
[2023-06-09T10:22:21.600Z]                     DeclaringType : initialsessionstate
[2023-06-09T10:22:21.600Z]                     MemberType    : Method
[2023-06-09T10:22:21.600Z]                     Module        : System.Management.Automation.dll

I pushed the project to Github here so you can see the errors it generates

https://github.com/brettmillerb/stackoverflowTest

Out of curiosity have you tried using the Azure Functions Deploy v2 task instead of a powershell task and using the PowerShell equivalent?

- task: AzureFunctionApp@2
  displayName: Azure Function App Deploy
  inputs:
    azureSubscription: $(azureSubscription)
    appName: samplefunctionapp
    appType: functionApp
    package: $(System.DefaultWorkingDirectory)/**/*.zip

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/azure-function-app-v2?view=azure-pipelines

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, yes I've tried both with the same result. something is different when publishing the function using vscode.
Thanks for the code. Looks like there is conflict loading it. You've given me some ideas - I'll come back to you. Thanks again
0

Instead of adding your Powershell Modules in a separate Module folder, Directly add them in requirements.psd1 folder, the requirements.psd1 will automatically download those packages for you.

I ran one Azure DevOps pipeline to deploy Azure Powershell HTTP Trigger and it was successful, Refer below:-

My repository:-

enter image description here

enter image description here

requirements.psd1

@{
    'Az' = '9.*'
    'Az.Accounts' = '1.1.0'
    'Az.Compute' = '0.5.0'
    'Az.Resources' = '6.7.0'
    'Az.Sql' = '4.7.0'
    'Az.SqlVirtualMachine' = '2.0.0'
}
trigger:
- main

variables:

  azureSubscription: 'xxxxxxxxx-52801a7de72a'

  functionAppName: 'siliconfunc5431'


  vmImageName: 'windows-2019'

  workingDirectory: '$(System.DefaultWorkingDirectory)/'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - powershell: |
        if (Test-Path "extensions.csproj") {
            dotnet build extensions.csproj --output ./$(workingDirectory)/bin
        }
      displayName: 'Build extensions'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: $(workingDirectory)
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: $(functionAppName)
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionApp
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

enter image description here

Portal:-

enter image description here

enter image description here

If you want to run the module files, Make sure you Import or Install them in a Powershell task like below and then run your Function Publish task.

Import-Module -Name Az.Accounts -Force
Import-Module -Name Az.Resources -Force
Import-Module -Name Az.Compute -Force
Import-Module -Name Az.Sql -Force
Import-Module -Name Az.SqlVirtualMachine -Force

If still the error persists, Run your Pipeline by enabling diagnostics or by adidng --Verbose flag at the end of your Powershell command like below:-

- task: AzurePowerShell@5
  displayName: 'Publish Function | azpwsh'
  inputs:
    azureSubscription: ${{ parameters.customerServiceConnection }}
    ScriptType: 'InlineScript'
    Inline: 'Publish-AzWebapp -ResourceGroupName $(functionAppRg) -Name $(functionName) -ArchivePath $(System.ArtifactsDirectory)/build$(Build.BuildId).zip -Force -Verbose'
    azurePowerShellVersion: 'LatestVersion'
    pwsh: true

1 Comment

Thanks, I have tried using the requirements.psd1 to install the modules I need, but it's very slow, hence why I opted to bundle the modules with the function. It's just so weird that it works publishing the function with vscode, but not with the AzDo tasks.
0

OK I got it working.

Although, I still don't understand why it wouldn't work using the Azure DevOps Task AzureFunctionApp@2 or the Azure PowerShell Task AzurePowerShell@5 using the command Publish-AzWebApp, but it would if it was published via vscode.

Basically, I streamlined the code (I didn't write originally), removed the modules and updated the following files:

host.json

Enabled managedDependency

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  },
  "functionTimeout": "00:10:00",
  "managedDependency": {
    "enabled": true // <----- This
  }
}

requirements.psd1

Removed 'Az' = '10.*' and added the following:

@{
    'Az.Accounts' = '2.12.3'
    'Az.Compute' = '6.0.0'
    'Az.Resources' = '6.7.0'
    'Az.Sql' = '4.7.0'
    'Az.SqlVirtualMachine' = '2.0.0'
}

Thanks for all in responding. Everyone gave me some ideas and I just ended up trying different configurations and enhancing the original code.

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.