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:
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”
I’ve tried to import the module when I create an endpoint in endpoints.ps1. :
New-PSUEndpoint -Url “/GetADCountries” -Endpoint {
Import-Module (Join-Path (Get-Location).Path “MyCustomModule.psm1”)
Custom-Function
}
Thanks for your reply Adam!
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?
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.