Slow module load

Hi,

I’ve written several custom PowerShell modules recently which provide wrappers around some az and mggraph commands. For some reason these modules are very slow to import. Like 50 seconds or so. The modules themselves only have 1 exported funciton and maybe 10-20 private functions all in one big file.

When I run import-moodule MYModule -verbose, all of the slowness appears to be before it exports the functions.

I tried removing the RequiredModules from the manifest, thinking this was slowing it, but it didn’t make a difference.

Is there a way of troubleshooting this slowness?

Here is a copy of the manifest and root module file:
Manifest

@{
# Script module or binary module file associated with this manifest.
RootModule = ‘ManageAzRbac.psm1’

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @(
    'Az.Accounts',
    'Az.Resources',
    'Az.ResourceGraph'
)

# Version number of this module.
ModuleVersion = '1.2.0'

# ID used to uniquely identify this module
GUID = '789287d1-02ff-4886-b2bd-c8c5171d4a8e'

# Author of this module
Author = 'Jeremy Hagan'

# Company or vendor of this module
CompanyName = 'Company Dot Com'

# Description of the functionality provided by this module
Description = 'Module to create and manage Access Group and Role Assignments in Azure'

# Functions to export from this module
FunctionsToExport = @('Set-AzRbac')

# Cmdlets to export from this module
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = @()

# Aliases to export from this module
AliasesToExport = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess
PrivateData = @{

}

}

Root Module

Dot source all public functions

Get-ChildItem -Path $PSScriptRoot\Public*.ps1 | ForEach-Object { . $_.FullName }

Dot source all private functions

Get-ChildItem -Path $PSScriptRoot\Private*.ps1 | ForEach-Object { . $_.FullName }

as a test, get a specific time for the current state

Measure-Command { Import-Module ManageAzRbac }

And then make a new iteration of the module where all of your public and private functions live in one monolithic .psm1 file (you could use a loop with Out-File -Append for this). Then measure importing that version of the module where all the functions are statically within the psm1 and report back.

If the speeds are significantly faster, then we can blame the dot sourcing. If they are not, there must be something else going on.

Hi,

Moving all the functions into the root module file did nothing to change the slow load time.

I just tried manually importing the 3 required modules you specify in your manifest and measuring their time, then I re-read your note and saw that you already tried removing them.
If you’re only exporting one public function, then I think all that remains is those private functions you mentioned.
You might try manually looping through all of those and measuring their import time to see if anything stands out:

Get-ChildItem -Path \ModuleFolderPath\Private*.ps1 | Foreach-Object {
    $Time = Measure-Command {. $_.FullName}
    [PSCustomObject]@{
        FileName = $_.Name
        ImportTime = $($Time.TotalSeconds)
    }
}

I think I have it. It IS the required modules which are slowing things up. The reason I still had the slowness when I removed the RequiredModules statement from the manifest was because I also had the same modules in the #requires statement in the script files.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.