convert array to enum

Welcome Forums General PowerShell Q&A convert array to enum

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 year ago.

  • Author
    Posts
  • #85282

    Participant
    Points: 38
    Rank: Member

    I have an array $computernames and need an enum [validComputernames]

    The solution in the thread below is not working for me and seems outdated since PS5.0 added the enum Keyword.
    https://powershell.org/forums/topic/create-enum-from-array/

    Any ideas?

  • #85285

    Participant
    Points: 38
    Rank: Member

    Ok, I found away using invoke-expression. Not very elegant but it works

    $computernames = Get-content C:\Paul2018\Clients.txt
    
    $exp = "Enum ValidComputernames{
        $(foreach ($name in $computernames){$name + ';'})
    }"
    
    Invoke-Expression $exp
    
  • #85294
    js

    Participant
    Points: 307
    Helping Hand
    Rank: Contributor
  • #85295

    Participant
    Points: 307
    Helping Hand
    Rank: Contributor

    How is your approach different / more efficient that what is documented here:
    'social.technet.microsoft.com/wiki/contents/articles/26436.how-to-create-and-use-enums-in-powershell.aspx'

The topic ‘convert array to enum’ is closed to new replies.