Class definitions or global variables

Product: PowerShell Universal
Version: 1.5.14

Hi!

I’d like my class definitions and common variables (or constants) to be shared among my APIs and Dashboards.
Where should I define them? What’s the best practice?

Thanks
Arnaud

I would suggest defining them in a common module and then using a shared environment and importing that module: Environments - PowerShell Universal

1 Like

Hmmm sounds definitely the perfect solution for me. :slight_smile:

Do you have any example somewhere? That would be helpful.

2 Likes

This video will walk you through it: https://youtu.be/FJmpfzOVYiA

You rocks Adam!!! :metal:

I created a module where I defined my functions, variables and classes. Then I created an environment where I import the module. My dashboard loads correctly the module as I specified my dashboard to use this environment (logs shows that the module has been correctly imported).

My problem is my classes are not imported in my dashboard, neither the variables. But my functions are available.
I guess the module import is not enough as normally we should use the using statement in order to import the classes and variables. I tried using “using” in the dashboard code but it seems there’s no effect.
How can I import my classes?

Hello :wink:

I think you can’t use the using statement, and import-module doesn’t import classes.

But you can import the functions in the same module where there is your classes. These functions have access to the classes. So you can define a function that returns an object created with the constructor of any of your classes.

For example :

class CEAOrg {
    CEAOrg([String] $Name) {
    }
}

function Get-CEAOrg {
   param([String] $name)

   return [CEAOrg]::new($name)
}

Export-ModuleMember -Function Get-CEAOrg