powershell 为什么Email和Office字段只输出system.object[]?

jtw3ybtb  于 2023-01-20  发布在  Shell
关注(0)|答案(1)|浏览(131)

我不明白为什么Email和Office列显示系统对象而不是各自的值?不明白为什么datafile 02中的值不起作用而datafile 01中的值起作用。

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

下面是我从上面的代码中得到的输出。

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[]

数据文件01

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

数据文件02

25328   bdbrown@att.net Memphis
57467   boser@verizon.net   Portland
55373   tfinniga@optonline.net  Kansas City
68730   meinkej@sbcglobal.net   Huntington
41186   tubajon@outlook.com Santa Ana
60961   dwheeler@contoso.com    Detriot

下面的代码块成功了!

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
    }

     }
juud5qan

juud5qan1#

类似下面这样的东西应该可以做到:

$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
    }
}

您必须在循环中选择所需的匹配数据行。

相关问题