Using modules in EndPoints

Hi There

Using UD 2.5.0 on OSX with Powershell Core

I’m trying to figure out what would be the best way to use modules in UD / EndPoints
For example, to use functions from a module I have written I import the module in the endpoint

New-UDGrid -Title “Servers” -Headers @(“Name”, “InstanceID”) -Properties @(“Name”, “InstanceID”) -Endpoint {
Import-Module -Name “…/Util/Modules/AWS.psm1” -DisableNameChecking -Force
Import-Module AWSPowerShell.NetCore
Set-DefaultAWSRegion -Region eu-west-1
$EC2Servers = Get-EC2Instance
$GridData = @()
foreach ($EC2Server in $EC2Servers) {
$EC2Name = Get-AWSTagValue -Tags $EC2Server.Instances[0].Tags -Key “Name”
$GridData += [PSCustomObject]@{
Name = $EC2Name
InstanceID = $EC2Server.Instances[0].InstanceId
}
}
$GridData | Out-UDGridData
}

As you might have guessed, this creates a grid full of server details. I f I do not import the modules then either the AWS cmdlets are not found or thge functions I wrote cannot be found.

But is there not a better way to include the modules globally in the dashboard then having to import them in each endpoint? I tried to use New-UDEndpointInitialization like so:

$EPModules = New-UDEndpointInitialization -Module @(“AWSPowerShell.NetCore”, “…/Util/Modules/AWS.psm1”)

And then load the dashoboard with the EndpointInitialization option like so:

New-UDDashboard -Title “Center” -Pages $Pages -EndpointInitialization $EndPoint_Modules

There are no error messages (I have logging turned on) but I get errors when I try to use functions/cmdlets from the modules.

So I was wondering, what is the best strategy to keep using modules and have them loaded only once in a dshboard session?

Are you using one script to run your whole dashboard? I’ve gone the route of just launching dashboard.ps1, then in that file I have all my declarations for themes, functions, modules and pages.

Hi!

I have dashboard.ps1 and in there I declare pages, run dashboard, etc.

But in the pages I load the specific ps1 for that page in the Content section, so I try to break the pages up into separate ps1 files.

When I load a module in that dashboard.ps1 I find that any code I use in the page specific ps1 it (the functions in the module that is) is not available/working for the elements I declare in those ps1 files, the endpoints
where I start coding the element is unaware. When I import a module inside the page specific ps1 the functions from the module are also not available in the Endpoints. They are for when the page .ps1 is loaded the first time but after that first time, those
are not re-executed of course. Hence I need to load them in the Endpoints themselves.

I have a UD project template that creates a module for every dashboard. in the .psm1 file you can keep all your functions and they will be available in every endpoint via the New-EndpointInitialization, which I also include. Really any function you include in the /src folder will be sourced.

1 Like

I saw this yesterday evening and I will certainly have a look at your project, it seems super helpeful!

The thing is, I also have “generic” modules I wrote stored in another location and used by other scripts. So these modules would be shared ideally and not copied to the dashboard folder structure. Would your ptoject be able to help with keeping the module in the location I have now and still include it?

Also, the module “AWSPowerShell.NetCore” is maybe different, as in I do not have to include a path to have it loaded?

So one would be : Import-Module -Name “…/Util/Modules/AWS.psm1”
And the other : Import-Module AWSPowerShell.NetCore

Or am I overcomplicating this?

Are your modules stored somewhere in the default module path? If so, they will automatically import when called. If not, that might be a consideration for you to append the location of these modules to your path.

For example, this would add your custom folder to your PS session temporarily (until you close the console). You could add it to your profile if you want.

$env:PSModulePath = "{0};{1} " -f ($env:PSModulePath,"$env:SystemDrive\Your\Module\Folder")

Alternately, you could create it in a persistant fashion:

$CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
[Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + ";$env:SystemDrive\Your\Module\Folder", "Machine")

Hi

Yep, they are stored I a folder which is not a module folder know to PowerShell

Never really saw the need as when I write script the are somewhere in that folder structure and I import them via $PSScriptRoot and then some additional folder information/location

But the location is not the problem I think, it is more about where to load them in UD and scoping, having the content/functions from the modules available in Content and Endpoint script blocks. I am really struggling
with that part and I cannot wrap my head around the rules, the logic escapes me for the moment.

Combine this with some odd UI behaviour I am having (New-UDGrid is fighting me a bit
:slight_smile:) and it makes for some frustrating learning curve
:slight_smile:

Met vriendelijke groet / Warm regards,

Steven Spierenburg

Ah gotcha.

On that front, it sounds like what you’re after is solved with New-UDEndpointIntialization cmdlet. This cmdlet has a -Module parameter that will take a path value.

New-UDEndpointInitialization -Module @("ActiveDirectory", "C:\Path\MyModule.psm1")

That will initialize your module in all runspaces.

additional info: https://docs.universaldashboard.io/endpoints/endpoint-initialization

Thanks for the help!

I tried using that New-UDEndpointInitialization feature

When I try to just name the module, as I would in a script (or in fact is loaded by default via my profile) like so:

Import-Module AWSPowerShell.NetCore
Set-DefaultAWSRegion -Region eu-west-1

This works, finds the right version and sets the default region via that cmdlet “Set-DefaultAWSRegion”

So when I tried the same via “New-UDEndpointInitialization” like so:

$EndPoint_Modules = New-UDEndpointInitialization -Module @(“AWSPowerShell.NetCore”, “/path/to/other/modules/test.psm1”)

The “test” module works, I can access the functions in that module
But when accessng any function/cmdlet from the module “AWSPowerShell.NetCore” they fail, unkown cmdlet appears on the screen

When I try to specify the module a bit more in detail, like so:

$EndPoint_Modules = New-UDEndpointInitialization -Module @("/Users/xxxxxx/.local/share/powershell/Modules/AWSPowerShell.NetCore/3.3.509.0AWSPowerShell.NetCore.psd1", “/path/to/other/modules/test.psm1”)

As you can see the path is a bit too specific for my taste (any update will break my dashoard I would say), but it does work

Then of course the next issue pops up :slight_smile:
That library wants to know the default region you work in so I want to set that globally as well. I cannot find a way to have this:

Set-DefaultAWSRegion -Region eu-west-1

Executed automatically for all Endpoints

Do you have any idea how I could achieve that?