# Importing Custom Modules for API

Product: PowerShell Universal
Version: 1.4.6


I’m trying to create an API that interacts with Active Directory that manages users. I’ve written functions and stored them in Powershell module files (.psm1). I’ve tried importing the modules so I can call the functions in the API endpoints but I’ve not been very successful.

Things I’ve tried:

1. Configured the Environment to import the modules. My environments.ps1 file looks like this:
New-PSUEnvironment -Name “ADModules” -Path “C:\Program Files\PowerShell\7\pwsh.exe” -Variables @(’*’) -PSModulePath @(‘path\to\module.psm1’)

I then selected it in the settings.ps1 file:
Set-PSUSetting -LoggingFilePath “C:\ProgramData\UniversalAutomation\logs\log.txt” -LogLevel “Information” -Telemetry -APIEnvironment “ADModules”

1. I’ve tried to import the module when I create an endpoint in endpoints.ps1. :
Import-Module (Join-Path (Get-Location).Path “MyCustomModule.psm1”)
Custom-Function
}

Any help appreciated

In your first example, are you using the full path? I would have expected that to work.

I’m not sure what the behavior will be in your second example as I’m not 100% sure what Get-Location will return in this case.

1 Like

I’m using the full path in the first example, pointing to the modules that are sitting on my C Drive. The behaviour is that the endpoint returns nothing because the function doesn’t seemingly exist. Are there any other things I have to do when I create the endpoint?

New-PSUEndpoint -Url “/test” -Endpoint {
}

The behaviour for the second example was strange. If I hard code the path it works, but the above approach only works some of the time, which leads me to believe that there are issues resolving the path.

Either way, are there some ways to debug the first approach? Or perhaps is there a workaround?

Strange. I would suggest trying to attach the debugger to the API to see what the current state is. It’s a couple steps but will greatly improve the debugging experience.

https://docs.powershelluniversal.com/debugging/debugging-scripts#debugging-scripts-from-a-powershell-console

You mostly just need to use Enter-PSHostProcess and Debug-Runspace and then you can see exactly what is going on in your API call.