skip to Main Content

I am trying to use a custom PowerShell module in an Azure Automation account.

I have prepared the module and tested it locally. It is loaded and the commands can be used.

Screenshot Local

However, when I pack and upload my module to Azure Automation it always imports the module without exporting the commands although the path is existing and the functions should be available.

Screenshot Azure Automation

My module consists of dedicated files for each functions located in the ‘functions’ folder of the module. I am using the following approach to load the functions via my .psm1 file:

$functions = @(Get-ChildItem -Path $PSScriptRootfunctions*.ps1 -ErrorAction SilentlyContinue)

foreach ($import in @($functions)) {
    try {
        . $import.Fullname -ErrorAction Stop
    }
    catch {
        Write-Error -Message "Failed to import function $($import.Fullname): $_" -ErrorAction Continue
    }
}

Export-ModuleMember -Function $functions.Basename

My .psd1 is created from a template:

 
#...
RootModule        = 'MHZ.Developer.psm1
#...
 FunctionsToExport = '*'
    CmdletsToExport   = '*'
    VariablesToExport = '*'
    AliasesToExport   = @()
#...

I tried commenting the values out, writing explicit function names or switching between wildcard and empty array without luck. According to blogs I read it also does not really matter since my .psm1 exports the members. Its only relevant for auto loading the cmdlets which I don’t really mind.

Shouldn’t that work in Azure Automation the same way it works locally or am I overseeing something?


UPDATE

My zip folder structure looks as followed:

MHZ.Developer.zip
├── MHZ.Developer.psd1
├── MHZ.Developer.psm1
│   ├── functions
│   │   ├── Get-FooBaa.ps1

Creating another folder with the module name in the zip does not make a difference. After uploading the module is always available under path C:usrsrcPSModulesMHZ.DeveloperMHZ.Developer.psm1

Changing the FunctionsToExport to something like:

FunctionsToExport = @(
'Get-Function1',
'Get-Function2',
'Get-Function3'
)

Does not make a difference either.

2

Answers


  1. Chosen as BEST ANSWER

    Solution

    By the help of the comments I could find the solution and answer my own question.

    Despite working locally I had to remove Export-ModuleMember from my .psm1 so it looks like this:

    $functions = @(Get-ChildItem -Path $PSScriptRootfunctions*.ps1 -ErrorAction SilentlyContinue)
    
    foreach ($import in @($functions)) {
        try {
            . $import.Fullname -ErrorAction Stop
        }
        catch {
            Write-Error -Message "Failed to import function $($import.Fullname): $_" -ErrorAction Continue
        }
    }
    

    and set FunctionsToExport in my .psd1 to all functions that should be available like this:

       FunctionsToExport = @(
            'Get-Function1',
            'Get-Function2',
            'Get-Function3'
        )
    

    to make the module working in Azure Automation.


  2. I’m having the same exact problem. I had the Export-ModuleMember in my psm1 file and have FunctionsToExport set to "*". The functions are being exported locally but when I upload to AA, nothing is exported. This was working for several years now. I updated one function earlier and when I try uploading to AA to update the module, it now doesn’t export any of the functions.

    I tried removing Export-ModuleMmeber from my psm1 file as suggested in this thr

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search