Use custom PowerShell defined type for parameter type specification

Question:

I define a custom PowerShell type with New-Object. I would like a parameter to be of my defined type, is it possible to specify this type in a declarative way? The following code gives me the error: “Unable to find type [BuildActionContext]: make sure that the assembly containing this type is loaded.”

Can we specify the type declarative, or should I just test the type of the specified object?

Not working code:

Working code, but can it be done differently:

Note: Second call gives the exception message.

Answer:

I would expect that only real .NET types can be used to specify parameter type. According to Essential PowerShell: Name your custom object types the custom type names are mainly used for formatting.

You can check the type names manually via ValidateScript attribute:

Source:

Use custom PowerShell defined type for parameter type specification by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply