I am building an automation system using Powershell and I would like to define custom .Net types so that I can structure my application in a similar way to the other code I write.
At first, I tried using ‘Add-Type’ to import types from c# files into my application but I found this to be extremely painful to debug as a .net type can only be loaded once during per Powershell session. This means that when there are errors a single change to my code requires me to restart my app (I might as well be using C#).
At the moment I am simulating types in Powershell by using factory functions that dynamically generate PSObjects with the semantics I need. This does work but it is very verbose and I don’t seem to be able to declare private variables or methods for encapsulation.
Is there an easier way to achieve what I want?
If not….
Am I somehow barking up the wrong tree by wanting to write my Powershell code using abstract data types?
Is there a better way to structure my system that I should know about?
Can I achieve some encapsulation with what I am doing now?
1) I haven’t tried it, but this project looks promising: PSClass – Object Oriented Scripting in Powershell. Some features it offers:
2) You could use custom modules like this:
Try to copy it to your ide/console and run it.
$script:varis private member that can be used only from functionsGetVar,AddToVar.checkRangeis private as well.Personally I wouldn’t use PowerShell for such a task if I needed real OOP (available in C#). PowerShell has some support for objects, but very poor for OOP.