Home > Enterprise >  Why does the Email and Office fields only output system.object[]?
Why does the Email and Office fields only output system.object[]?

Time:01-02

I can't understand why the Email and Office columns show system object instead of the respective values? Can't figure out why the values from datafile02 aren't working but the values from datafile01 are.

Set-StrictMode -Version latest
$txt1 = "C:\Users\Alex\Downloads\New folder\datafile01.txt"
$txt2 = "C:\Users\Alex\Downloads\New folder\datafile02.txt"
$newtxt = "C:\Users\Alex\Downloads\New folder\new.txt"

#Import Data from TXT and add headers
$d1 = import-csv $txt1 -Header UID,HDATE,EMAIL,OFFICE -Delimiter `t | Select-Object -Property UID,EMAIL,HDATE,OFFICE 
$d2 = import-csv $txt2 -Header UID2, EMAIL, OFFICE -Delimiter `t | Select-Object -Property EMAIL, OFFICE

#Create new Table and Columns
$table = New-Object system.Data.DataTable "Employee Table"
$newcol = New-Object system.Data.DataColumn UID; $table.columns.add($newcol)
$newcol = New-Object system.Data.DataColumn EMAIL; $table.columns.add($newcol)
$newcol = New-Object system.Data.DataColumn HDATE; $table.columns.add($newcol)
$newcol = New-Object system.Data.DataColumn OFFICE; $table.columns.add($newcol)

 # Add a DataTable row
 $row = $table.NewRow()
 $row.UID= ($d1.UID)
 $row.EMAIL= ($d2.email)
 $row.HDATE= ($d1.HDATE)
 $row.OFFICE= ($d2.OFFICE)
 $table.Rows.Add($row)
 
 #Grab data from TXT
 $d1 | ForEach-Object {
     $row = $table.NewRow()
     $row.UID= ($_.UID)
     $row.EMAIL= ($d2.EMAIL)
     $row.HDATE= ($_.HDATE)
     $row.OFFICE= ($d2.OFFICE)
     $table.Rows.Add($row)
     } 

# Get the data out
 $table

Here is the output I'm getting from the code above.

UID             EMAIL           HDATE           OFFICE         
---             -----           -----           ------         
System.Object[] System.Object[] System.Object[] System.Object[]
25328           System.Object[] 5/31/2023       System.Object[]
57467           System.Object[] 9/14/2023       System.Object[]
55373           System.Object[] 1/21/2023       System.Object[]
41186           System.Object[] 6/15/2023       System.Object[]
60961           System.Object[] 3/14/2023       System.Object[]

Datafile01

25328   5/31/2023
57467   9/14/2023
55373   1/21/2023
41186   6/15/2023
60961   3/14/2023

Datafile02

25328   [email protected] Memphis
57467   [email protected]   Portland
55373   [email protected]  Kansas City
68730   [email protected]   Huntington
41186   [email protected] Santa Ana
60961   [email protected]    Detriot

The following code block worked!

Set-StrictMode -Version latest
$txt1 = "C:\Users\Alex\Downloads\New folder\datafile01.txt"
$txt2 = "C:\Users\Alex\Downloads\New folder\datafile02.txt"


#Import Data from TXT and add headers
$d1 = import-csv $txt1  -Delimiter `t -Header UID, SDate

$d2 = import-csv $txt2 -Delimiter `t -header UID, EMAIL, OFFICe

$d1 | ForEach-Object {
     $DataMatch = $d2 | Where-Object -Property UID -eq $_.UID
     [PSCustomObject]@{
        UID    = $_.UID
        Date   = $_.SDate
        Email  = $DataMatch.Email
        OFFICE = $DataMatch.OFFICE
    }

     } 



CodePudding user response:

Something like this should do the trick:

$DataFile1 = Import-Csv .\DataFile1.txt -Delimiter "`t" -Header UID, Date
$DataFile2 = Import-Csv .\DataFile2.txt -Delimiter "`t" -Header UID, EMAIL, OFFICE

$DataFile1 |
ForEach-Object {
    $DataMatch = $DataFile2 | Where-Object -Property UID -EQ $_.UID
    [PSCustomObject]@{
        UID    = $_.UID
        Date   = $_.Date
        Email  = $DataMatch.Email
        OFFICE = $DataMatch.OFFICE
    }
}

You have to select the desired matching data row inside the loop.

  • Related