Powershell-导出CSV和追加 [英] Powershell - Export-CSV and Append

查看:510
本文介绍了Powershell-导出CSV和追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下脚本:

$in_file = "C:\Data\Need-Info.csv"
$out_file = "C:\Data\Need-Info_Updated.csv"
$list = Import-Csv $in_file 
ForEach ( $user in $list ) {
$zID = $user.zID
ForEach-Object { Get-QADUser -Service 'domain.local' -SearchRoot 'OU=Users,DC=domain,DC=local' -SizeLimit 75000 -LdapFilter "(&(objectCategory=person)(objectClass=user)(PersonzID=$zID))" |
Select-Object DisplayName,samAccountName,@{Name="zID";expression={$zID}} | 
Export-Csv $out_file -NoTypeInformation -Force
}
}

但是,我无法获得将所有结果输出到$ out_file的信息,因为它似乎没有将数据追加到csv文件中.我尝试了许多在网上找到的东西,想法和建议.他们似乎都没有工作,我觉得我在这里很容易错过一些东西.有没有一种方法可以将数据追加到文件中?如果是这样,您可以提供一个示例或一段代码以使其以这种方式工作吗?我不是要重写所有代码或沿这些行的任何内容,而只是要求将要导出的数据附加到输出文件中.

However, I am not able to get it to output all of the results to the $out_file since it does not seem to append the data to the csv file. I have tried a number of things, ideas, and suggestions found online. None of them seem to work and I feel like I am missing something rather easy here. Is there a way to make this append the data to a file? If so, could you provide an example or a snippet of code to make it work in that manner? I am not asking for a rewrite of all of the code or anything along those lines, just a way to make the data that is being exported appended to the file that is output.

推荐答案

将foreach循环转换为foreach对象,并将export-csv移至foreach外部对象之外,以便可以将所有对象通过管道传递至export- csv.

Convert the foreach loop into a foreach-object and move the export-csv to outside the outter foreach object so that you can pipe all the objects to the export-csv.

类似的东西(未经测试):

Something like this (untested):

$list | ForEach {
$zID = $_.zID
ForEach-Object { Get-QADUser -Service 'domain.local' -SearchRoot 'OU=Users,DC=domain,DC=local' -SizeLimit 75000 -LdapFilter "(&(objectCategory=person)(objectClass=user)(PersonzID=$zID))" |
Select-Object DisplayName,samAccountName,@{Name="zID";expression={$zID}} | 

}
} |Export-Csv $out_file -NoTypeInformation -Force

这篇关于Powershell-导出CSV和追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆