Powershell Functions, how do you handle them in PSU

Powershell Functions, how do you handle them in PSU

avatar
(anonymous user)
Product: PowerShell Universal
Version: 2.7.0


Just wondering how people handle their functions in PSU especially functions that are use across scripts. .
Do you convert them all to API’s even if you only use them within your scripts?
Do you have a script will all your functions you call?
Do you modify PS profile to load your functions with every instance?
Or Do you just add the function to the top of each script?

Not sure if this would be overkill but was thinking about a feature request to add a new menu under Automation called functions. In here you can create your functions. Then in each script there is a field like Tags that allows you to attach the functions you want for that script so it knows what to load. Again not sure if this would be overkill to manage functions or not. Interested to see what other people are doing.

Thanks
Mike

All Comments (12)

avatar

You can do this with modules right now. If you go to the Platform \ Modules page, you can create a module and add your functions. Modules created in here will automatically be imported into all environments.

docs.powershelluniversal.com

Adam Driscoll
PowerShell Expert and Developer at Devolutions

avatar

So I tried to create a module and getting error! logs show the below.

System.NullReferenceException: Object reference not set to an instance of an object.
   at Universal.Server.Controllers.ModuleController.Post(Module module) in D:\a\universal\universal\src\Universal.Server\Controllers\ModuleController.cs:line 174
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 134
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Universal.Server.Middleware.SwaggerAuthenticationMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in D:\a\universal\universal\src\Universal.Server\Middleware\SwaggerAuthMiddleware.cs:line 42
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<<UseMiddlewareInterface>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context) in D:\a\universal\universal\src\AspNetCoreRateLimit\Middleware\RateLimitMiddleware.cs:line 109
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
2022-01-07 12:18:36.178 -05:00 [WRN] No exception handler was found, rethrowing original exception.
2022-01-07 12:18:36.179 -05:00 [ERR] Connection id "0HMEH76ABCJV6", Request id "0HMEH76ABCJV6:0000000E": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Universal.Server.Controllers.ModuleController.Post(Module module) in D:\a\universal\universal\src\Universal.Server\Controllers\ModuleController.cs:line 174
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 134
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Universal.Server.Middleware.SwaggerAuthenticationMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in D:\a\universal\universal\src\Universal.Server\Middleware\SwaggerAuthMiddleware.cs:line 42
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<<UseMiddlewareInterface>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context) in D:\a\universal\universal\src\AspNetCoreRateLimit\Middleware\RateLimitMiddleware.cs:line 109
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Universal.Server.UniversalDashboardStartup.<>c.<<Configure>b__5_0>d.MoveNext() in D:\a\universal\universal\src\Universal.Server\UniversalDashboardStartup.cs:line 132
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)


avatar

Hmmm. I just tried this myself and it worked. Can you let me know what you entered? I can reproduce this if I enter an invalid version number.

This works:



8942e884d2fba35e45aa1c2f3348a0cb31ce0109
This does not work:

Name: asdfasdfas
Version: asdfasdfas

Adam Driscoll
PowerShell Expert and Developer at Devolutions

8942e884d2fba35e45aa1c2f3348a0cb31ce0109.png

avatar

1641776a753babe64a5439a9be717fe570b2f886


2f67f4dc11662b07f646ffd664b0a97bf9f625ef


I do have other modules installed. Could one of them be causing a problem?
Mike

1641776a753babe64a5439a9be717fe570b2f886.png

2f67f4dc11662b07f646ffd664b0a97bf9f625ef.png

avatar

I’m not sure. It seems like it’s not getting that far based on the log message you are seeing. I will have to play around with it to see if I can reproduce.

I’ll let you know if I need some more info.

In the mean time, you could try to manually create the module. Modules added to the Repository\Modules folder will be automatically added to the PSModulePath in your environments.

Adam Driscoll
PowerShell Expert and Developer at Devolutions

avatar

So i went to the folder and it created the folder’s and the psd1 and the psm1 files when i use the gui. When i search in the modules for the name it does not come up though. Not sure if related but the PowerShell galley still does not work for us but I think I saw a post on that so I was not worried about it

Created customModule
0ab9a6fd0a4ec08a3d856124c2840de7eee73a36



9fec10153b3131825ead0620239f5a94787c85a3

dc7ef51af0b9b3e77bd5b7c0bd842f836a6bc2bf

dc7ef51af0b9b3e77bd5b7c0bd842f836a6bc2bf.png

9fec10153b3131825ead0620239f5a94787c85a3.png

0ab9a6fd0a4ec08a3d856124c2840de7eee73a36.png

avatar

For testing I emptied out the module folder. It now let me create a module. Will add the modules back to see what one it was.
Mike

avatar

Ok, if I put any other module in that folder it stops working. like say Azuread. I have an older install that we used for poc before we bought the tool. While the powershell gallary does work on that one… it has the same problem.
What I notice is the “Universal Modules” disappears from the GUI as soon as I add azuread module.
With a custom one made from the gui.


97c2a7d2b05c673689540ac99f0716e73cfe5a5c
after i add other modules like active directory, sqlserver


305b5b44e20f324ed045f9b66f327a293ccf67cb
when I add msonline it gets weird.


978cd7842acc13b182d171e7bfca7af8a0d2650a
and azuread gets even weirder and the universal modules goes away all together.


4a88e64ad75c4183eaabccacaa71d55035e32b42
Hope this helps.
Mike

4a88e64ad75c4183eaabccacaa71d55035e32b42.png

978cd7842acc13b182d171e7bfca7af8a0d2650a.png

305b5b44e20f324ed045f9b66f327a293ccf67cb.png

97c2a7d2b05c673689540ac99f0716e73cfe5a5c.png

avatar

Thanks. That’s super helpful. I’ll open an issue for this and track it down.

Adam Driscoll
PowerShell Expert and Developer at Devolutions

avatar

Thank you for the quick responses. I apologize for all the posts. We are excited to move all our scripts and jobs to this tool, right now they all over the place. This will give us a lot more visibility and all the features provide endless possibilities.
Mike

avatar
module

Hey Adam, I am still seeing this problem, ( we upgraded to version 2.90 today)


efac81acd8346dd482a0941b91e37a1e9fcd93c5
as soon as I add more modules it messes up again.


ac4a2ce743c1b3ea521d4f0b5558641386e57426

Mike

ac4a2ce743c1b3ea521d4f0b5558641386e57426.png

efac81acd8346dd482a0941b91e37a1e9fcd93c5.png

avatar

v4.2.1
Go to Create Module, fill in a title and version and you get an exception:

Value cannot be null. (Parameter ‘s’)