Module Manifest

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sean@midnightdba.com
Posts: 14
Last visit: Tue Jun 13, 2023 7:20 pm
Been upvoted: 1 time

Module Manifest

Post by sean@midnightdba.com »

I'm trying to add a file to the module manifest but I can't get it to work.
I've got a file with a custom type and while I can add it as an include, I should be able to put it in the manifest under ScriptsToProcess. This section is supposed to load files under the session scope. But it doesn't see the custom type. The code is really simple and is attached.
Any ideas?
PSInterfaces.zip
(2.84 KiB) Downloaded 179 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Module Manifest

Post by jvierra »

It seems to load fine for me. How do you know it is not loading? The issue seems to be that "[Talker]" is never defined. I think you are defining it before you have defined "ICanTalk". The PSM1 executes first and before the PS1 is loaded. The PS1 defines the type as an interface exposure. The class must be defined before declaring the interface binding type.
User avatar
sean@midnightdba.com
Posts: 14
Last visit: Tue Jun 13, 2023 7:20 pm
Been upvoted: 1 time

Re: Module Manifest

Post by sean@midnightdba.com »

Here's what I get when I run it.
InterfaceError.png
InterfaceError.png (28.33 KiB) Viewed 5344 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Module Manifest

Post by jvierra »

I have no issues. This is how to load the module.
Screenshot 2022-04-25 234101.png
Screenshot 2022-04-25 234101.png (9.08 KiB) Viewed 5317 times
User avatar
sean@midnightdba.com
Posts: 14
Last visit: Tue Jun 13, 2023 7:20 pm
Been upvoted: 1 time

Re: Module Manifest

Post by sean@midnightdba.com »

Sorry it took so long to reply, I didn't see the checkbox for reply emails.
Well, the point is that I want to be able to run the script and have the script load everything for me. This will be part of an automated process so I won't be able to separately load the module like that.
Besides, this is what ScriptsToProcess in the psd1 is for? Or even the ModulesToLoad section? Do you have any insight as to why that's not working?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Module Manifest

Post by jvierra »

You are not using the manifest; you are loading with a "using" command. Try putting the pieces into the manifest. aYou also seem to be trying to create a type without executing the code.

This:
[b}Add-Type -TypeDefinition "public interface ICanTalk{string talk(); }" -Language CSharp;[/b]

I suggest placing the type into a DLL as code rather than trying to inject it into memory using text methods. It looks like you have a circular dependency preventing either type from being created on module load. Also, I am not clear about why you are trying to create an "interface" type. This is not available when you try to reference one. You have to define the class before you define the interface to the class.
User avatar
sean@midnightdba.com
Posts: 14
Last visit: Tue Jun 13, 2023 7:20 pm
Been upvoted: 1 time

Re: Module Manifest

Post by sean@midnightdba.com »

So you're telling me that the manifest isn't processed unless I use import-module? The docs I've seen say that the diff between import-module and using module is that using module exports classes and import-module doesn't.
Is that not true?

And I'm not sure how I have a circular dependency. I ended up here cause I read that if I put the type declaration at the top of the script with the class, that the class gets loaded first and the type has to already be there when the class is loaded. So all I'm trying to do is get the type loaded into the session so I can create the class.
There's surprisingly little written on this topic.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Module Manifest

Post by jvierra »

No. I am not saying that. The load order tries to define the "interface" before the class has been created. The "Add-Type" statement is in a file that is specified as the script to execute before the module is loaded. Move both pieces of code into the PSM1 module. They should not be separated. They are part of the same script component. This will force them to be created in the correct order.

It looks like you are trying to use methods of building from Visual Studio to PowerShell modules. They are not alike. In VS code is chunked to objects then the build process matches and orders the pieces. PS cannot do that. Scripting systems parse in one order and do not solve recursive or out-of-order definitions like a multi-pass compiler/builder does.
User avatar
sean@midnightdba.com
Posts: 14
Last visit: Tue Jun 13, 2023 7:20 pm
Been upvoted: 1 time

Re: Module Manifest

Post by sean@midnightdba.com »

Well the reason I'm here is cause I had the both in the same file and it didn't work. When I wrote the psm1 originally I got the same error. Then I read that PS loads the classes first, and then the other stmts. So the type def has to be loaded into the session before the classes.
And I just tried it again and I still get an error. Have you tried it? If not, I'd like to see the results. Just make sure you reload your PS window first so the type isn't in the session.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Module Manifest

Post by jvierra »

You cannot define the typedef before the class is created. As I noted there is no compiler to resolve in multiple passes.
This topic is 1 year and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked