如何将输出附加到 CSV 文件
how to append output to a CSV file
foreach ( $newfile in $file )
{
$b = Get-CMDeploymentStatus -PackageId $newfile -StatusType Any | select PackageID
Write-Output $b | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\list.csv"
}
我正在使用一个输入文件对此进行输入,其中列出了多个包名称,然后我想以一种输出一个接一个的方式处理它,现在我收到一个错误
Export-Csv:无法将参数绑定到参数 'InputObject',因为它为空。在 line:16 char:20 + 写入输出 $b | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\lis ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
您的代码假设您将从 $b
返回一个结果,如果没有,您将收到一个错误,因为您正在管道 $b
,它是 null , 变成 Export-CSV
。
$null |export-csv c:\temp.csv
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At line:1 char:8
+ $null |export-csv c:\temp.csv
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCs
您应该在尝试导出之前添加 'Guard Clause'。
if ($null -ne $b){
Export-csv -Append -InputObject $b
}
至少这会继续执行。现在你的下一个问题是确定为什么 $b
会是空的......根据我使用 CM 的经验,我敢打赌你需要在你的 $file
中指定你需要的 属性。也许那一行应该是:
$b = Get-CMDeploymentStatus -PackageId $newfile.PackageId -StatusType Any | select PackageID
既然你说 "I am giving input to this with an input file which has number of package names listed",但你的代码使用 PackageId
..
在我看来,此文件包含一个 packageId,每个都在一行上。
无论如何,我没有看到读取此文件的代码..
如果我对文本文件的假设是正确的,请尝试:
# read the content of the text file and loop through the lines
# collect the output from Get-CMDeploymentStatus in variable $result
$result = Get-Content -Path 'X:\TheFileWithPackageIds.txt' | ForEach-Object {
# inside the ForEach-Object, the $_ automatic variable represents a single line from the text file
Get-CMDeploymentStatus -PackageId $_ -StatusType Any | select PackageID
}
# output on screen
$result
# write to new CSV file
$result | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\list.csv" -NoTypeInformation
foreach ( $newfile in $file )
{
$b = Get-CMDeploymentStatus -PackageId $newfile -StatusType Any | select PackageID
Write-Output $b | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\list.csv"
}
我正在使用一个输入文件对此进行输入,其中列出了多个包名称,然后我想以一种输出一个接一个的方式处理它,现在我收到一个错误
Export-Csv:无法将参数绑定到参数 'InputObject',因为它为空。在 line:16 char:20 + 写入输出 $b | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\lis ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
您的代码假设您将从 $b
返回一个结果,如果没有,您将收到一个错误,因为您正在管道 $b
,它是 null , 变成 Export-CSV
。
$null |export-csv c:\temp.csv
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At line:1 char:8
+ $null |export-csv c:\temp.csv
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCs
您应该在尝试导出之前添加 'Guard Clause'。
if ($null -ne $b){
Export-csv -Append -InputObject $b
}
至少这会继续执行。现在你的下一个问题是确定为什么 $b
会是空的......根据我使用 CM 的经验,我敢打赌你需要在你的 $file
中指定你需要的 属性。也许那一行应该是:
$b = Get-CMDeploymentStatus -PackageId $newfile.PackageId -StatusType Any | select PackageID
既然你说 "I am giving input to this with an input file which has number of package names listed",但你的代码使用 PackageId
..
在我看来,此文件包含一个 packageId,每个都在一行上。
无论如何,我没有看到读取此文件的代码..
如果我对文本文件的假设是正确的,请尝试:
# read the content of the text file and loop through the lines
# collect the output from Get-CMDeploymentStatus in variable $result
$result = Get-Content -Path 'X:\TheFileWithPackageIds.txt' | ForEach-Object {
# inside the ForEach-Object, the $_ automatic variable represents a single line from the text file
Get-CMDeploymentStatus -PackageId $_ -StatusType Any | select PackageID
}
# output on screen
$result
# write to new CSV file
$result | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\list.csv" -NoTypeInformation