PowerShell导出csv结果与列 [英] PowerShell exporting csv results with columns

查看:474
本文介绍了PowerShell导出csv结果与列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要有一个csv作为输出与一列中的服务器的名称和命令的结果在第二列。我不能使用export-csv cmdlet,因为im使用旧版本的PS。非常感谢您的帮助!

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. I cannot use the export-csv cmdlet because im using a old version of 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


推荐答案

p>因此,在循环中使用字符串格式化程序,并使用-append out-file。或者不要使用ForEach($ server在$ servers)和管道它所有的out-file,但你不能轻松管道foreach循环,因为你有它的构造。只要确保先添加标题行即可。

So then use a string formatter in the loop and out-file with -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. 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

这篇关于PowerShell导出csv结果与列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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