none
Pulling hash out of loops (foreach) RRS feed

  • Question

  • Hi,

    Hope this is the right group.  I have encountered variants of this off and on for some time, and would like to find a good explanation of what is going on.  In this case, I have a file with what I'll call "userid" on it, one per line:

    user01

    user02

    user03

    Right now, I'm reading it with import-csv, but get-content should work as well.  I read the file, then pass each userid to the get-mailbox cmdlet to obtain the PrimarySMTPAddress and DisplayName. 

    $list = Import-Csv -Path path/file.csv -header "address"
    ForEach ($address in $list) {
         $address
    #    get-mailbox -identity $address |select-object PrimarySMTPaddress,displayName
    }
    

    If I execute as above, using $address, I will get

    address

    user01

    user02

    user03

    If I uncomment the Get-Mailbox cmdlet and use $address as the Identity parm, I get:

    Cannot process argument transformation on parameter 'Identity'. Cannot convert value "@{Address=user01}" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".

    Error: "Cannot convert hashtable to an object of the following type: Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not supported

    in restricted language mode or a Data section."

    I am sure I am just overlooking something here, but shouldn't it be fairly straightforward to pass these? I realize that data types gives powershell a lot of power.  Most examples I see of Foreach provide examples more like the "$address" above, but how to pull the value out of the hash?  While I may be overlooking something, this might also be a more complex topic than it seems, so any links on the subject are appreciated.

    Thanks,

    --Joe 

    Tuesday, October 14, 2014 10:11 PM

Answers

  • Of course; just use the column name as the property of the object. For example:


    $csvData = @(
      new-object PSObject -property @{
        "Name" = "Value 1"
      }
      new-object PSObject -property @{
        "Name" = "Value 2"
      }
    )
    foreach ( $row in $csvData ) {
      $row.Name
    }
    # Output:
    # Value 1
    # Value 2
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, October 14, 2014 10:33 PM
    Moderator

All replies

  • If you use Import-Csv, you will get objects.

    If you use Get-Content, you will get strings.

    I recommend using Get-Content. Then you don't have to get object properties to get the string values.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Wednesday, October 15, 2014 12:14 AM
    Tuesday, October 14, 2014 10:15 PM
    Moderator
  • Thanks Bill,

    This solves the immediate problem, but if I did have to work with a csv (it happens) can the value still be pulled out? 

    --Joe

    Tuesday, October 14, 2014 10:26 PM
  • Of course; just use the column name as the property of the object. For example:


    $csvData = @(
      new-object PSObject -property @{
        "Name" = "Value 1"
      }
      new-object PSObject -property @{
        "Name" = "Value 2"
      }
    )
    foreach ( $row in $csvData ) {
      $row.Name
    }
    # Output:
    # Value 1
    # Value 2
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, October 14, 2014 10:33 PM
    Moderator
  • Aw geeze, of course.  Thanks again.
    Tuesday, October 14, 2014 10:37 PM