Sometimes a message shows that a function could not be found

This happens sometimes and I cannot figure it out. Happened on all versions of UD I tried, currently I am on UD 2.9 at the moment, hosted on Windows 2012R2 with PowerShell 5.1

$PSVersionTable

Name Value


PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

At times I get the following message (taken from the UD log file):

The term ‘GenerateNewOHCNumber’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again

The way the dashboard is structured is as follows:
Dashboard defines side nav and items route to a page ps1
The pages do a dot source the appropriate code ps1 to have flexible source editing capabilities without having to restart the dashboard after I made an edit
In the page code ps1 I have a New-UDCard defined with the visuals, one being a button to generate a number.
The button has a OnClick section where the function GenerateNewOHCNumber is called.

I have tried to place that function on the top as well as the bottom of the page code ps1, does not make a difference.

If I do a page refresh (F5) then in all probability the issue is gone.
But it will return at times, making it unpredictable and a bit annoying to the users using the dashboard.

Does this sound familiar to anyone and is there perhaps a solution to this?

Picture with the message:

Hey Steven,
Have you defined your function in the endpoint initialization? This will make it available anywhere within UD rather than having to place it within pages themselves as this can lead to problems depending on how the page is constructed. You can also add all of your functions to the endpoint initialization as well as any modules or variables you might want to use across the site.

Here’s a simple example:

function New-MyFunction {
“Do something”
}

function New-MyFunction2 {
“Do something else”
}

$EndpointInitialisation=New-UDEndpointInitialization -Function @(
“New-MyFunction”,
“New-MyFunction2”
)

Then add the endpoint initialization into your “New-UDDashboard” command

New-UDDashboard -EndpointInitialization $EndpointInitialization

And you’ll now be able to call “New-MyFunction” and “New-MyFunction2” anywhere within your app.

Hopefully that helps!

Hi Tom,

Thanks for the information, appreciated!
I did know about the Variables and Modules loading in endpoint initialisation, never used it for functions though. My more generic functions go into the specific Dashboard module I created.
Functions applying only to the page itself I keep in that ps1 file of that page.

Oddly enough, with version 2.9 I did see that behaviour but I also did a cleanup of some code and changed some logic and I did not see it come back after this. I will keep an eye out for this and when I move to version 3 of Universal I hope this is also a thing of the past.

But quick check, it is not forbidden to have several functions on the script file for a page, is it? Forbidden is a strong word but perhaps discouraged? I could not find any info on this.

Again, thanks for your suggestions and feedback!

Regards,
Steven

Hi Steven,
No worries, I’m not sure what the best practice is to be honest! I suppose the tidiest way would be to put all of your functions in a module and then import the module. I only have a few that I use so I just put them on the main file and load them into the endpoint initialization with the functions param.