PowerCLI

 View Only
  • 1.  Switch statement and hash table

    Posted Jun 21, 2013 02:21 PM

    All,

    I had a question in regards to the switch statement and hash tables.  I know with a switch statement you can do an array like so

    $a = 21, 38, 6

    switch ($a)

    { 1 {"The color is red."}

    2 {"The color is blue."}

    3 {"The color is green."}

    4 {"The color is yellow."}

    5 {"The color is orange."}

    6 {"The color is purple."}

    7 {"The color is pink."}

    8 {"The color is brown."} }

    I actually have found an example that I use to evaluate a hash table based on the $_.name value

    $myHash = @{}

    $myHash["a"] = 1

    $myHash["b"] = 2

    $myHash["c"] = 3

    switch ($myhash.GetEnumerator())

    { {$_.name -like ‘*a*’ } {"It is a"} }

    What I want to understand is how does the curly braces know to do a comparison on the $_.name variable?  How come you don't have to use the Where statement?  I didn't find any documentation on the swtich statement that went over hash tables.  IS there an advanced guide for powershell on syntax for things like this?

    Also, is there a good powershell forum out there?  google only shows technet and I'm not a big fan of that format. 



  • 2.  RE: Switch statement and hash table

    Posted Jun 21, 2013 04:30 PM

    The trick is in the GetEnumerator method that is called.

    Through that method the Switch doesn't see a hash table, but a simple object that has a Name and a Value property.

    You can check by doing

    $myhash.GetEnumerator() | Get-Member

    I hope that didn't confuse you more :smileygrin:



  • 3.  RE: Switch statement and hash table

    Posted Jun 21, 2013 04:34 PM

    Gotcha, now on that note, if you pass an object to the switch statement, how does it know to evaluate $_.name?  Don't you need a where statement in there?  Is it hte curly braces that is used to evaluate that?



  • 4.  RE: Switch statement and hash table

    Posted Jun 21, 2013 04:39 PM

    I think i've got it now.  I think it makes more sense here if I break it down.  So you can do a script block for Switch to evaluate if true and if you have an object you can do "object -eq "string" " and powershell knows to evaluate and return true?

    Man I don't think I could figure out that logic on my own :smileyplain:

    $myhash.GetEnumerator() | where {$_.name -eq "c"
    }

    Name                           Value
    ----                           -----
    c                              3


    $hash = $myhash.GetEnumerator() | where {$_.name -eq "c"}
    $hash

    Name                           Value
    ----                           -----
    c                              3


    $hash.Name
    c

    $hash.Name -eq "c"

    True



  • 5.  RE: Switch statement and hash table
    Best Answer

    Posted Jun 21, 2013 06:40 PM

    That is correct.

    Have a look at the section about the Switch statement in Tobias's free ebook.

    It explains all the possibilities rather well.



  • 6.  RE: Switch statement and hash table

    Posted Jun 21, 2013 06:44 PM

    Excellent that looks like a good resource!



  • 7.  RE: Switch statement and hash table

    Posted Jun 21, 2013 06:46 PM

    It is, a high quality document (and free).