I created a custom module according to the documentation here that includes a basic function declaration. I tried using the function in one of my dashboards, but no joy. I checked the dashboard log and I see that only modules in the binary folder are loading, but modules in C:\ProgramData\UniversalAutomation\Repository\Modules are not being loaded.
I restarted the dashboard and the PowerShell Universal Windows service, but it still isn’t working. The “Creating Modules” section in the documentation says, “these modules will be available in all environments.”
Product: PowerShell Universal
Version: 2.12.5
MSI (Windows service) version
I just tried that to no avail (see attached screenshots). The dashboard I’m working on has the functions defined within it and it works, but when I comment out the function declarations things break (i.e. data no longer displays).
Yes! It shows that it loaded, but it’s showing it loaded multiple times. Check out lines 210 and 213 for the .psm1 file in the attached screenshot. I also noticed that the .psd1 file only loaded one (1) time.
Well, it’s loading. If you are using the integrated environment, maybe it does autoload (I’m not 100% on what the integrated environment will load automatically).
That leaves us with: your module is the problem.
If you post the sanitized code I can take a look. There is a difference in scopes when using a full blown module, defining a bunch of functions in the script, or dot sourcing a ps1 file.
It’s still not working, but, to your point, scoping might, in fact, be an issue in this case.
As I stated before, everything works with the functions defined directly in the dashboard. One thing to note is that several functions make calls to other functions; all of which are defined in the dashboard. For example, the function I’ve been testing (Get-AADUserDetails) is called by the function Get-UserDetails.
It stops working when I comment out the function declaration for Get-AADUserDetails in the dashboard; effectively forcing the use of the function defined in the module.
Perhaps I need to change the scope of something somewhere along the way or rewrite the dashboard to eliminate the nested function calls.
Without seeing any code, I can’t really recommend any path forward. Nested functions are fine, however you need to make sure the scopes are passing the right data between each other.
Edit:
One thing for sure is that if you are setting variables in the function that you intend to use in the dashboard, that won’t work (and is troublesome for maintaining). Make sure your functions are returning data, which you can then assign to a variable.
I don’t know if it helps but I also have sometimes in a powershell 5 environment the phenomenon that a function is now and then reported as missing when calling the dashboard. What has caused me now and then to define the function again directly on site bzw in the area of the UDDynamic. Is certainly not really good but my errors are gone
@adam, I hope you can assist with this issue. I’ve tried everything I can think of to get a module I created to work, but I’m having no luck.
First a couple of questions:
I put the function names in the FunctionsToExport section of the .psd1 file and I included Export-ModuleMember for each function in the .psm1 file. Is it necessary to do both or is it one-or-the-other?
How do you get Modules to work in PSU v2.12.5? According to the v2 documentation “[modules created directly in PowerShell Univeral] will be available in all environments.” I receive an error in my dashboard stating that “Find-ADUser” is not recognized. I’ve tried including the entries described in the prior bullet as well as Import-Module in my dashboard. The dashboard log shows the following: Mar 27, 2023 5:02 PM Loading module: C:\ProgramData\UniversalAutomation\Repository\Modules\Disable-Users\1.0.0\Disable-Users.psd1
I put the function names in the FunctionsToExport section of the .psd1 file and I included Export-ModuleMember for each function in the .psm1 file. Is it necessary to do both or is it one-or-the-other?
If you have a PSD1, use the FunctionsToExport. You shouldn’t need both methods.
Can you try checking the $Env:PSModulePath in the dashboard environment? It should be setting the repository directory (C:\ProgramData\UniversalAutomation\Repository\Modules).
New-UDTypography $ENV:PSModulePath
If not, I would recommend trying to set it to see if that makes a difference.
@adam Below are the entries. I’m only including the ones related to PSU/UD. I’m not sure why there are multiple entries for the same path or why there are entries for locations other than the one you specified.