I'm trying to convert a string to a enum value in PowerShell but couldn't find this anywhere...
I'm getting a JSON result, where I only want to consume the Healthstate which is defined as a string.
enum HealthState
{
Invalid = 0
Ok = 1
Warning = 2
Error = 3
Unknown = 65535
}
$jsonResult = "Ok"
$HealthStateResultEnum = [Enum]::ToObject([HealthState], $jsonResult)
Thanks in advance.
You can simply cast the string result as the Enum type:
$HealthStateResultEnum = [HealthState]$jsonResult
This will work whether $jsonResult
contains a string or value from the enum type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With