试图获得@ {Name =输出结果

时间:2018-03-13 17:35:17

标签: powershell

我有一个脚本,它有一个自定义PSObject数组,每个只有2个项目。我需要对每个对象中的第一个值进行排序,然后吐出两个项目,但它们都是@{Name=@{Line=。这是我的代码片段:

$expinfo = @()
<<<>>>
$dbinfo = New-Object -TypeName PSObject
$dbname = Get-Item $folder | select Name
$dbinfo | Add-Member -NotePropertyName DBName -NotePropertyValue "$dbname"

$dbinfo | Add-Member -NotePropertyName ExportLine -NotePropertyValue "$eline"
$expinfo += $dbinfo
<<<>>>
foreach ($expdb in ($expinfo.GetEnumerator() | Sort-Object DBName))
{
    $dbn = $expdb | Select-Object -ExpandProperty DBName
    $dbe = $expdb | Select-Object -ExpandProperty ExportLine
    $dbn
    $dbe
}

输出如下:

@{Name=AIT1PD}
@{Line=20180312.1700 20180312.1704 All items successfully completed.}
@{Name=APAC1PD}
@{Line=20180313.0100 20180313.0120 All items successfully completed.}

我希望它看起来像这样:

AIT1PD 20180312.1700 20180312.1704 All items successfully completed.
APAC1PD 20180313.0100 20180313.0120 All items successfully completed.

1 个答案:

答案 0 :(得分:2)

更改您的Add-Member语句,以添加您感兴趣的属性值:

$dbinfo | Add-Member -NotePropertyName DBName -NotePropertyValue $dbname.Name
$dbinfo | Add-Member -NotePropertyName ExportLine -NotePropertyValue $eline.Line

假设您使用的是PowerShell 3.0或更高版本,我建议您使用[pscustomobject]类型加速器:

$dbinfo = [pscustomobject]@{
    DBName     = (Get-Item $folder).Name
    ExportLine = $eline.Line
}