team!
I have variable with type PSObject[] in my advanced function.
[Parameter( Mandatory = $false, Position = 0, HelpMessage = "PsObject data." )]
[PSobject[]] $data,
...
but sometimes my input $data with type [string[]] is transforming to [PSObject[]] and i catch error while im using object property.
im trying to validate it by script
[Parameter( Mandatory = $false, Position = 0, HelpMessage = "PsObject data." )]
[ValidateScript({ ( ( $_ -is [PSobject] ) -or ( $_ -is [PSobject[]] ) -or ( $_ -is [System.Object[]] ) ) })]
$data,
but it has no effect, i see the $data with type [string[]], i`am continue to cath errors.
Whats wrong?
Edit: based on the comments, it sounds like your real question is:
For that, you need to exclude two kinds of input values:
[datetime]
's, anything passed by value in .NET really)(As mklement0's excellent answer shows, properties can be added to local copies of these types - but PowerShell cannot predictably "resurrect" them when passing values between adjecent commands in a pipeline, among other quirks)
You can validate that input objects do not fall in one of these buckets, like this:
PSObject
is a generic wrapper type that PowerShell uses internally to keep track of extended properties and members attached to existing objects.For this reason, any object can be converted to
PSObject
implicitly - in fact, PowerShell does so every time an object passes from one command to another across|
in a pipeline statement - and it has no real effect in terms of enforcing specific input object traits.If you want to ensure that an object has specific properties, the best option is to define a specific datatype with the
class
keyword:You can now pass instances of the declared type to the function parameter:
But PowerShell can also implicitly convert custom objects to the desired target type if they have matching properties:
If you want to validate the existence of specific properties and potentially their value without explicit typing, you have to access the hidden
psobject
memberset of the object in the validation script - note that it'll validate one item at a time:Now, if we pass an object with a
RequiredProperty
property that has some value, the validation succeeds: