1

I have an array of custom objects:

$report = @()

foreach ($person in $mylist)
{
  $objPerson = New-Object System.Object

  $objPerson | Add-Member -MemberType NoteProperty -Name Name -Value $person.Name
  $objPerson | Add-Member -MemberType NoteProperty -Name EmployeeID
  $objPerson | Add-Member -MemberType NoteProperty -Name PhoneNumber

  $report += $objPerson
}

Note that I haven't set values for the last two properties. The reason I've done this is because I'm trying to produce a matrix where I'll easily be able to see where these are blanks (although I could just set these to = "" if I have to).

Then, I want to iterate through a second dataset and update these values within this array, before exporting the final report. E.g. (this bit is pretty much pseudo code as I have no idea how to do it:

$phonelist = Import-Csv .\phonelist.csv

foreach ($entry in $phonelist)
{
  $name = $entry.Name
  if ($report.Contains(Name))
  {
    # update the PhoneNumber property of that specific object in the array with
    # another value pulled out of this second CSV
  }
  else
  {
    # Create a new object and add it to the report - don't worry I've already got
    # a function for this
  }
}

I'm guessing for this last bit I probably need my if statement to return an index, and then use that index to update the object. But I'm pretty lost at this stage.

For clarity this is a simplified example. After that I need to go through a second file containing the employee IDs, and in reality I have about 10 properties that need updating all from different data sources, and the data sources contain different lists of people, but with some overlaps. So there will be multiple iterations.

How do I do this?

2 Answers 2

2

I would read phonelist.csv into a hashtable, e.g. like this:

$phonelist = @{}
Import-Csv .\phonelist.csv | ForEach-Object { $phonelist[$_.name] = $_.number }

and use that hashtable for filling in the phone numbers in $report as you create it:

$report = foreach ($person in $mylist) {
  New-Object -Type PSObject -Property @{
    Name        = $person.Name
    EmployeeID  = $null
    PhoneNumber = $phonelist[$person.Name]
  }
}

You can still check the phone list for entries that are not in the report like this:

Compare-Object $report.Name ([array]$phonelist.Keys) |
  Where-Object { $_.SideIndicator -eq '=>' } |
  Select-Object -Expand InputObject
Sign up to request clarification or add additional context in comments.

2 Comments

Please see added detail in question. I still think this will work, I'll try it out and let you know. But in the meantime if you think there's a better way of doing it given the added detail please let me know.
I'd use the same approach for all data. Pick a unique attribute that can serve as a lookup key. Load all the additional data into hashtables using the unique attribute as the key. Generate your report and fill the properties via hashtable lookups while you generate it. There might be some mixing and matching required, depending on what your actual data look like, but this is the general approach.
0

I would iterate over the $phonelist two times. The first time, you could filter all phone entities where the name is in your $myList and create the desired object:

$phonelist = import-cse .\phonelist.csv
$report = $phonelist | Where Name -in ($mylist | select Name) | Foreach-Object {
    [PSCustomObject]@{
        Name = $_.Name
        PhoneNumber = $_.PhoneNumber
        EmployeeID = ''
    }
}

The second time you filter all phone entities where the name is not in $myList and create the new object:

$report +=  $phonelist | Where Name -NotIn ($mylist | select Name) | Foreach-Object {
    #Create a new object and add it to the report - don't worry I've already got a function for this
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.