Home > Enterprise >  PowerShell Invoke returns array instead of int
PowerShell Invoke returns array instead of int

Time:10-21

I have the following snippet of PowerShell:

$property = @{
    Getter = { 80 };
}

$value = $property.Getter.Invoke() 
$value.GetType() # Shows Name = Collection'1 instead of int

I would expect $value.GetType() to return Int32 but instead it returns a collection.

I can't just take element [0] because sometimes the Getter function will return an array.

How can I fix this?

CodePudding user response:

You can strictly declare the type of the return value by this way. For example, the return type will be Double :

cls
$property = @{
    Getter = [Func[Double]]{ 80 }
}

$value = $property.Getter.Invoke() 
$value.GetType().Name
# Double
  • Related