从对象输出 PowerShell 中删除 @{} 标记



我需要帮助从对象输出中删除@{}扩展名。

下面的代码列出了文件夹中最后修改的文件。但是输出在扩展@{}中。

我已经尝试了输出字符串,但它不起作用。

function scriptA() {
Get-ChildItem $path | Where-Object {!$_.PsIsContainer} | Select fullname -last 1
}
function scriptB() {
Get-ChildItem $path2 | Where-Object {!$_.PsIsContainer} | Select fullname -last 1
}
$data1=ScritA
$data2=ScriptB
$result=@()
$list=@{
FirstFile=$data1
SecondFile=$data2
}
$result+= New-Object psobject -Property $list
$result | Export-Csv -append -Path  $csv

这将输出:FirstFile@{data1} 和SecondFile@{data2}

稍微改变一下你的函数 -

function scriptA() {
Get-ChildItem $path | Where-Object {!$_.PsIsContainer} | Select-Object -ExpandProperty fullname -last 1
}
function scriptB() {
Get-ChildItem $path2 | Where-Object {!$_.PsIsContainer} | Select-Object -ExpandProperty fullname -last 1
}

这样做将允许您仅选择FullName属性。

如果不想更改函数,请将$list分配更改为 -

$list=@{
FirstFile = $data1.FullName
SecondFile = $data2.FullName
}
New-Object PSObject -Property @{FirstFile = ($a = (Get-ChildItem $path1),(Get-ChildItem $path2) |
Where-Object {!$_.PSIsContainer} | ForEach-Object {$_[-1].FullName})[0];SecondFile = $a[1]} |
Export-Csv $csv -NoTypeInformation

最新更新