如何在PowerShell中将数据导出到CSV?

sof*_*fun 20 powershell powershell-2.0

foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {
        foreach ($file in $REMOVE) {
            Remove-Item "\\$computer\$DESTINATION\$file" -Recurse
            Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"            
        }
    } else {
        Write-Host "\\$computer\$DESTINATION\"
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将Write-Host"\ $ computer\$ DESTINATION \"导出到CSV文件,以便我知道脚本运行时哪些计算机处于脱机状态.

我是从Windows 7机器上运行的

Dav*_*tin 31

此解决方案创建一个psobject并将每个对象添加到一个数组中,然后通过Export-CSV管理数组的内容来创建csv.

$results = @()
foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {
        foreach ($file in $REMOVE) {
            Remove-Item "\\$computer\$DESTINATION\$file" -Recurse
            Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"            
        }
    } else {

        $details = @{            
                Date             = get-date              
                ComputerName     = $Computer                 
                Destination      = $Destination 
        }                           
        $results += New-Object PSObject -Property $details  
    }
}
$results | export-csv -Path c:\temp\so.csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

如果将字符串对象传递给csv,则会将其长度写入csv,这是因为这些是字符串的属性,有关详细信息,请参阅此处.

这就是我首先创建一个新对象的原因.

请尝试以下方法:

write-output "test" | convertto-csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

这会给你:

"Length"
"4"
Run Code Online (Sandbox Code Playgroud)

如果在写输出上使用Get-Member,如下所示:

write-output "test" | Get-Member -MemberType Property
Run Code Online (Sandbox Code Playgroud)

你会看到它有一个属性 - 'length':

   TypeName: System.String

Name   MemberType Definition
----   ---------- ----------
Length Property   System.Int32 Length {get;}
Run Code Online (Sandbox Code Playgroud)

这就是为什么将长度写入csv文件.


更新:附加CSV 如果文件变大,则不是最有效的方法...

$csvFileName = "c:\temp\so.csv"
$results = @()
if (Test-Path $csvFileName)
{
    $results += Import-Csv -Path $csvFileName
}
foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {
        foreach ($file in $REMOVE) {
            Remove-Item "\\$computer\$DESTINATION\$file" -Recurse
            Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"            
        }
    } else {

        $details = @{            
                Date             = get-date              
                ComputerName     = $Computer                 
                Destination      = $Destination 
        }                           
        $results += New-Object PSObject -Property $details  
    }
}
$results | export-csv -Path $csvFileName -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

  • 作为一个小改进,您可以使用$ details =**[ordered]**@ {...这将确保csv文件中的列以指定的顺序显示. (2认同)

小智 5

只需使用Out-File cmd,但不要忘记提供编码类型: -Encoding UTF8

所以使用它是这样的:

$log | Out-File -Append C:\as\whatever.csv -Encoding UTF8
Run Code Online (Sandbox Code Playgroud)

如果要多次写入文件,则需要-append。