无法处理参数,因为参数“名称”的值无效

时间:2019-03-22 14:26:41

标签: powershell

我试图将变量的结果放入一个对象,该对象最终输出到这样的csv文件中

$output | Add-Member -MemberType NoteProperty -Name "Merk" -Value $Desktop.CsManufacturer
$output | Add-Member -MemberType NoteProperty -Name "Model" -Value $Desktop.CsModel
$output | Add-Member -MemberType NoteProperty -Name "S/n" -Value $Desktop.BiosSeralNumber

$output | Export-Csv -Path $GL\info.csv -NoTypeInformation -Append -Force

(这只是整个代码的一部分)

我的目标是拥有一个文件,我可以首先在USB记忆棒上运行,然后再从AD启动脚本运行在PC上,该脚本从Desktop,Monitor,OS等提取数据,并将其放入CSV文件,但我总是收到错误“由于参数“名称”的值无效,因此无法处理参数”。这意味着我想上面的代码中name的值有问题吗?我仍然是这方面的菜鸟,所以我只是尝试一切。

或者,如果有人认为我很挣扎并且对如何在CSV文件中输出变量有更好的主意,请告诉我。

1 个答案:

答案 0 :(得分:1)

代替使用“添加成员”创建自定义PSObject,请尝试以下操作:

$OutPutArray = @()
foreach ($computer in $complist) {
    $output = [Ordered]@{
        "Make" = $computer.CsManufacturer
        "Model" = $computer.CsModel
        "S/n" = $computer.BiosSerialNumber
    }
    $OutPutArray += New-Object PSOBject -Property $output
}
$OutPutArray | Export-csv <path to file> -NoTypeInformation

EDIT 建议使用[PSCustomObject]替代方案,并直接分配foreach的输出。

$OutPutArray = foreach ($computer in $complist) {
    [PSCustomObject]@{
        "Make"  = $computer.CsManufacturer
        "Model" = $computer.CsModel
        "S/n"   = $computer.BiosSerialNumber
    }
}
$OutPutArray | Export-csv <path to file> -NoTypeInformation

生成的对象相同,但效率更高。

相关问题