header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Hash table
Last Post 16 Aug 2010 09:59 AM by cameronove. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
uSlackrUser is Offline
New Member
New Member
Posts:68
Avatar

--
16 Aug 2010 07:52 AM
    I'm trying to turn a short csv file into an hash table so I can use it for lookup in my script.  It is not working, so I assume I don't understand something.

    I have a two column csv with columns 'Supervisor' and 'Manager DN'.  I tried to build a hash with the supervisor as the key and the ManagerDn as the value.  To do so I tried this: (note: $ss has the contents of the csv imported using Import-csv)

    $supdn=@()
    $ss | foreach { $supdn[$_.Supervisor] = "$_.Manager DN"}


    When I do that, then try to retrieve the value of one of the keys using this:
       $supdn["Baer,Bob P"]

    I get this:
    @{Supervisor=Baer,Bob P; Manager DN=CN=bbaer,OU=User Accounts,OU=Ourtown,DC=sometech,DC=com}.Manager DN

    It appears the value of the key is itself a hash table.

    I verified the contents of the $ss variable as this:
    >$ss
    Supervisor            Manager DN
     ----------            ----------
    Abbey,David K.  CN=Dabbey,OU=User Accounts,OU=OurTown,DC=Sometech,DC=com
    Babu,Daniel M    CN=Dxbabu,OU=User Accounts,OU=OurTown,DC=Sometech,DC=com
    Ball,Sam A          CN=Sball,OU=User Accounts,OU=OurTown,DC=Sometech,DC=com
    Baer,Bob P         CN=mbaer,OU=User Accounts,OU=OurTown,DC=Sometech,DC=com



    Can you help me understand what I'm missing?

    Thanks

    \\Greg
    \\uSlackr
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    16 Aug 2010 08:10 AM
    • Accepted Answer
    @() is an empty array
    @{} is an empty hashtable

    Try:

    $supdn = @{}
    $ss | foreach{$supdn += @{$_.Supervisor = $_.'Manager DN'}}
    uSlackrUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    16 Aug 2010 09:01 AM

    Interesting. Using the @{} is certainly necessary, but it also appears using the $_.'Manager DN' vs "$_.Manager DN" is necessary.  Any idea why?

    BTW, I was able to us this syntax for adding the entries vs "+="
    $ss | $supdn[$.Supervisor] = $_.'Manager DN'}


    Thanks, Cameron

    \\uSlackr
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    16 Aug 2010 09:16 AM
    Yes, there is a space in the property name. If that is the case you need to specify the property in either single or double-quotes. On a side note and something I use quite often is that properties can be expressed in variables as well. This allows you to get values from properties dynamically which can be very useful.

    i.e.
    $propertyname = 'Supervisor'
    $myobject.$propertyname = 'My supervisor'

    $myobject.Supervisor -eq 'My supervisor' would return True.


    Regarding your syntax, that is correct I've used that on occasion as well.
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    16 Aug 2010 09:27 AM
    Oh and one other thing you can also do this:

    $ss | %{$supdn.($_.Supervisor) = $_.'Manager DN'}

    you can add to a hash table like this:

    $myhash.NewName = 'NewValue'

    If NewName is not part of the hashtable it will add the name and assign the value to the new name.

    Hash tables are really cool in powershell.

    In your example this would also work (just for demonstration--not suggesting this method)

    $ss | %{$Supervisor = $_.Supervisor;$Manager = 'Manager DN';$supdn.$Supervisor = $_.$Manager}

    Obviously that is a lot of extraneous code, but it demonstrates nicely what can be done with hash tables and properties.

    uSlackrUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    16 Aug 2010 09:42 AM
    Cameron,
    On the quoting of the variable with spaces, I expected that "$_.Manager DN" would work as I thought the variable name would get expanded in the double quotes. In more testing, I see that what I wound up with was "$_" being replace with the current object and the ".Manager DN" being appended as text.

    Curious result, but I get it.

    Thanks again for the assist
    \\uSlackr
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    16 Aug 2010 09:59 AM
    If you want to use a variable with a string; in you case you'd do this:

    make sure you use double-quotes in the outside quotation marks or it won't work (double-quotes resolve variables and single-quotes do not).

    "$($_.'Manager DN')"

    Even inside the double-quotes you still need the single quotes around the property name or you will get an error.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer