Powershell Foreach循环未导出为csv

Powershell Foreach循环未导出为csv,powershell,foreach,export-to-csv,Powershell,Foreach,Export To Csv,PowerShell脚本 $(foreach ($k in $sql_output){ $ip = $k[0] $present = $k[1] write-output $ip, $present }) | export-csv D:\Script\is_present.csv 我希望is_present.csv看起来像 10.10.10.10 1 10.10.10.11 1 10.10.10.12 0 相反,它看起来像 #TYPE Syst

PowerShell脚本

$(foreach ($k in $sql_output){
    $ip = $k[0]
    $present = $k[1]
    write-output $ip, $present   
}) | export-csv D:\Script\is_present.csv
我希望is_present.csv看起来像

10.10.10.10    1
10.10.10.11    1
10.10.10.12    0
相反,它看起来像

#TYPE System.DBNull
如何修复?

试试这个

 $(foreach ($k in $sql_output){ 
       $ip = $k[0] 
       $present = $k[1]
       $out = "{0,20}{1,30} -f $ip, $present
       Write-output $out
     }) | out-file D:\Script\is_present.csv

循环与以前的循环基本相同
Export Csv
可以更好地处理对象,因此不知道您的数据来自何处,并且可以使用
[pscustomobject]
处理对象(需要PowerShell 3.0)。获取该数据并将其传递到下一个管道,以便
导出Csv
-NoTypeInformation
将停止将System.Management.Automation.PSCustomObject作为文件的第一行。

如何使$ip和$present输出在同一行上。当我执行这个命令时,我得到一行$ip,下一行$present,下一行$ip,下一行$present,etc@waliatoumi现在,$ip和$present输出在同一行,但在同一列。如何在不同的列上生成$ip和$present输出。我试着把
放在$ip和$present之间,但仍然不能解决问题。这个答案非常有用
$(foreach ($k in $sql_output){ 
    [PsCustomObject]@{
        'IP' = $k[0]
        'Present' = $k[1]
    }
}) | Export-Csv c:\temp\temp.csv -NoTypeInformation