将两行的结果合并为 table

Combine results from two lines for a table

我有一个 PowerShell 脚本,它基于 'computer' 列中的 CSV 提取计算机信息。从这些信息中,我试图从两个来源查询相关信息。

一个是显示登录时间戳的文本文件,另一个是来自 AD 的 lastlogondate。我想将结果放在一起以 CSV 格式输出。

我不知道如何合并结果。单独的命令被放入 $table 变量中,但这显然是不正确的。

所以我想要最终得到的是

的 CSV
Computer,LastWriteTime,LastLogonDate
PC01,lastwritetime,ADLastLogonDate

我在想解决方案是将值写入彼此独立的变量,然后将它们一起放入 $table 变量中?但我不确定该怎么做。

$computerList = Import-Csv $path

$table = @()

foreach ($line in $computerList) {
    $compName = $line.computer

    # Get the LastWriteTime from the network drive
    $table += Get-ChildItem *"$compName"* -Path R: |
              sort LastWriteTime |
              select LastWriteTime, Name  -Last 1

    # Get the lastlogondate from Active Directory
    $table += Get-ADComputer $compName -Properties Name, LastLogonDate |
              select Name,LastLogonDate
}

$table | Export-Csv "file.csv"

未测试

$computerList = import-csv $path
$table = @()
$table = foreach($line in $computerList){
    [pscustomobject]@{
        Computer      = $line.computer
        LastWriteTime = get-childitem -Path R: "*$compName*"|
                          sort LastWriteTime -descending|
                          select -ExpandProperty LastWriteTime -first 1
        LastLogonDate = get-adcomputer $compName -properties Name,LastLogonDate|
                          select -ExpandProperty LastLogonDate
    }
} 
$table | out-gridview
# $table | export-csv "file.csv"

根据建议再次编辑