Manipulating Array or variable

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 3 months ago.

  • Author
    Posts
  • #17996
    Profile photo of Juli
    Juli
    Participant

    My challenge for the day.

    I am importing a csv. Each entry has 4 properties. The first property is completed and I need to use that property to obtain the other properties. I will be doing this in a foreach loop but apparently I don't quite understand how to get that information into the variable.

    When I import my csv ($test) I end up with this:

    email SipAddress sAMAccountName Mailbox
    somevalue
    somevalue
    somevalue
    somevalue

    I was hoping that I could do something like this to fill in the values for SipAddress, sAMAccountName, Mailbox for use later on in the script. And yes they have to be together because at one point I need to take a returned value (Mailbox) and compare it against the variable to be able to pass the sAMAccountName to another command:

    foreach ($i in $test)

    {$test.sAMAccountName = get-aduser -property sAMAccountName}

    I have tried this a couple different ways can't quite get it. Not sure what I need to do to get the information I want. Maybe create a new variable?

  • #17998
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    It looks like you're on the right track. This bit is a problem, though:

    foreach ($i in $test)
    {
        $test.sAMAccountName = get-aduser -property sAMAccountName
    }
    

    $test, here, is an array. What you would want to be changing is a property of $i, something like this:

    foreach ($i in $test)
    {
        $user = Get-ADUser -Filter "emailAddress -eq '$($i.email)'"
        $i.SamAccountName = $user.SamAccountName
    
        # Other code to set SipAddress and Mailbox properties in $i in a similar way.
    }
    

    At this point, you've modified objects in memory. Later, you need to export them back to CSV:

    $test | Export-Csv -NoTypeInformation -Path .\someFile.csv
    

You must be logged in to reply to this topic.