How to convert a string to a enum? How to convert a string to a enum? powershell powershell

How to convert a string to a enum?


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.


Assuming you want to get the value__ of the enum:

PS> [Enum]::GetValues([HealthState])|? {$_ -eq $JSonresult}|Select @{n="Name";e={"$_"}},value__Name value__---- -------Ok         1

Or simply

PS> [int]([HealthState]$jsonResult)1