Types in PowerShell 2 Modules

I implemented a small PowerShell module that brings a special type with it. I defined the type in the .psm1 file as a C # class and added it using Add-Type . Now, when I add a module and delete it again, the type still exists, which is probably not entirely correct (for example, it prevents the module from being added again). The documentation for Remove-Module states that types defined in assemblies loaded by the module are also unloaded. But my module does not bring assemblies, but just one tiny view in the form of source code.

I could just put the type in my own DLL and mark it as an assembly to load in the module manifest, but I like how the whole source code is now easily visible. Distributing a DLL with a module may raise a suspicion of why it needs an executable.

Is there something that I can connect to to somehow remove this type when unloading the module? Or should I just ignore potential errors with Add-Type in order to at least be able to re-add the module after being removed from the session? I would prefer not to put the DLL there (maybe all the same for this tiny module).

+4
source share
1 answer

The documents on the Remove-Module also say that the assembly is not unloaded. This is a fundamental problem with .NET and the CLR. When an assembly is loaded into AppDomain, it cannot be unloaded. Therefore, creating your own DLL (managed assembly) will not help.

I'm not sure that you can do a lot here without avoiding Add-Type and creating your own type using new-object psobject -prop @{...} and $obj.psobject.typenames.insert(0, 'newtypename')

+6
source

Source: https://habr.com/ru/post/1335573/


All Articles