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.
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.
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 $PSScriptRoot\functions\*.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?
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:\usr\src\PSModules\MHZ.Developer\MHZ.Developer.psm1
Changing the FunctionsToExport
to something like:
FunctionsToExport = @(
'Get-Function1',
'Get-Function2',
'Get-Function3'
)
Does not make a difference either.
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 $PSScriptRoot\functions\*.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.