如何在 PowerShell 中将数据导出到 CSV? [英] How to export data to CSV in PowerShell?

查看:85
本文介绍了如何在 PowerShell 中将数据导出到 CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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\"
    }
}

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

I want to export Write-Host "\$computer\$DESTINATION\" to the CSV files so I know which computers were offline when the script ran.

我在 Windows 7 机器上运行这个

I am running this from a Windows 7 machine

推荐答案

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

This solution creates a psobject and adds each object to an array, it then creates the csv by piping the contents of the array through Export-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

如果您将字符串对象通过管道传输到 csv,您会将其长度写入 csv,这是因为这些是字符串的属性,请参阅 此处 了解更多信息.

If you pipe a string object to a csv you will get its length written to the csv, this is because these are properties of the string, See here for more information.

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

This is why I create a new object first.

尝试以下操作:

write-output "test" | convertto-csv -NoTypeInformation

这会给你:

"Length"
"4"

如果您在 Write-Output 上使用 Get-Member 如下:

If you use the Get-Member on Write-Output as follows:

write-output "test" | Get-Member -MemberType Property

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

You will see that it has one property - 'length':

   TypeName: System.String

Name   MemberType Definition
----   ---------- ----------
Length Property   System.Int32 Length {get;}

这就是 Length 将写入 csv 文件的原因.

This is why Length will be written to the csv file.

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

Update: Appending a CSV Not the most efficient way if the file gets large...

$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

这篇关于如何在 PowerShell 中将数据导出到 CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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