简体   繁体   English

PowerShell导出带有列的CSV结果

[英]PowerShell exporting csv results with columns

i just need to have an csv as output with the name of the servers in one column and the result of the command in the second column. 我只需要有一个csv作为输出,第一列中的服务器名称,第二列中的命令结果。 I cannot use the export-csv cmdlet because im using a old version of PS. 我无法使用export-csv cmdlet,因为即时通讯使用的是PS的旧版本。 Thank you very much for your help! 非常感谢您的帮助!

$servers = Get-Content -path .\Server_List.txt

ForEach ($server in $servers)
{
(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
}
Out-File UAC_audit_results.csv

So then use a string formatter in the loop and out-file with -append. 因此,请在循环中使用字符串格式化程序,并使用-append输出文件。 Or don't use a ForEach($server in $servers) and pipe it all to out-file, but you can't easily pipe a foreach loop as you have it constructed. 或不要使用ForEach($ servers中的$ server)并将其全部管道传输到文件外,但是在构造它的过程中,您不能轻易地通过管道传输foreach循环。 Just make sure to add a header line first. 只要确保首先添加标题行即可。

$servers = Get-Content -path .\Server_List.txt
"Server,Results"|Out-File UAC_audit_results.csv
ForEach ($server in $servers)
{
    $RegProp = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
    "{0},{1}" -f $server, $RegProp|Out-File UAC_audit_results.csv -append
}

Or, (what I think is better), pipe it: 或者,(我认为更好)用管道输送:

"Server,Results"|Out-File UAC_audit_results.csv
Get-Content -path .\Server_List.txt |%{"{0},{1}" -f $_, (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System |select -expand EnableLUA)}|Out-File UAC_audit_results.csv -append

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM