如何通过管道将写入输出导出到导出 CSV? [英] How can I pipe Write-Output to Export-Csv?
问题描述
我有以下ping 脚本",我想在其中使用数组和 Export-Csv
cmdlet 将输出写入 CSV 文件而不是 txt 文件.>
我首先尝试将 Write-Output
管道传输到 Export-Csv
直到读到您不能将字符串管道传输到 Export-Csv
并且必须将它们转换首先进入对象.
我不太明白如何在 PowerShell 中使用数组.
参数([Parameter(Mandatory=$true, position=0)][string]$csvfile)$ColumnHeader = "主机名"写主机读取文件"$csvfile$ipaddresses = Import-Csv $csvfile |选择对象 $ColumnHeader写主机开始 Ping.."foreach ($ipaddresses 中的 $ip) {if (Test-Connection $ip.("Hostname") -Count 1 -Quiet) {写输出 $ip.("Hostname") "Ping 成功."|Out-File -FilePath "C:\temp\pingresults.txt" -Append} 别的 {写输出 $ip.("Hostname") "Ping 失败."|Out-File -FilePath "C:\temp\pingresults.txt" -Append}}写主机Ping 已完成".
当然,您可以通过管道将字符串导入 Export-Csv
.只是结果可能不是您所期望的,因为 Export-Csv
将输入对象的属性导出为输出文件的字段.;) String 对象只有一个属性 (Length
),因此您最终会得到一个列出输入字符串长度的输出文件.
要获得列出主机名(或 IP 地址)以及 ping 结果的输出 CSV,您需要构造具有 2 个属性的对象.在 PowerShell 中执行此操作的最佳方法是管道:
导入-Csv $csvfile |ForEach-Object {New-Object -Type PSObject -Property ([ordered]@{主机名 = $_.主机名Online = [bool](Test-Connection $_.Hostname -Count 1 -Quiet)}} |导出-Csv 'C:\path\to\output.csv' -NoType
由于您基本上只想向输入数据添加属性,您可以采用更简单的方法并使用 计算属性:
导入-Csv $csvfile |选择对象主机名,@{n='Online';e={[bool](Test-Connection $_.Hostname -Count 1 -Quiet)}} |导出-Csv 'C:\temp\pingresults.csv' -NoType
不推荐在循环中追加到数组,因为追加本质上会创建一个大小增加的新数组,从旧数组中复制所有元素,然后将新元素添加到新(空)槽并释放旧数组.
I have the following "ping script" in which I would like to use an array along with the Export-Csv
cmdlet to write the output to a CSV file rather than a txt file.
I first tried piping Write-Output
to Export-Csv
until reading that you cannot pipe strings to Export-Csv
and must convert them into objects first.
I don't quite understand how to work with arrays in PowerShell.
Param(
[Parameter(Mandatory=$true, position=0)][string]$csvfile
)
$ColumnHeader = "Hostname"
Write-Host "Reading file" $csvfile
$ipaddresses = Import-Csv $csvfile | Select-Object $ColumnHeader
Write-Host "Started Pinging.."
foreach ($ip in $ipaddresses) {
if (Test-Connection $ip.("Hostname") -Count 1 -Quiet) {
Write-Output $ip.("Hostname") "Ping succeeded." |
Out-File -FilePath "C:\temp\pingresults.txt" -Append
} else {
Write-Output $ip.("Hostname") "Ping failed." |
Out-File -FilePath "C:\temp\pingresults.txt" -Append
}
}
Write-Host "Pinging Completed."
Of course you can pipe strings into Export-Csv
. It's just that the result probably won't be what you expect, since Export-Csv
exports the properties of the input objects as the fields of the output file. ;) String objects only have one property (Length
), so you'd end up with an output file listing the lengths of the input strings.
To get an output CSV that lists the hostname (or IP address) along with the ping result you need to construct objects with 2 properties. The best way to do this in PowerShell is a pipeline:
Import-Csv $csvfile | ForEach-Object {
New-Object -Type PSObject -Property ([ordered]@{
Hostname = $_.Hostname
Online = [bool](Test-Connection $_.Hostname -Count 1 -Quiet)
}
} | Export-Csv 'C:\path\to\output.csv' -NoType
Since you basically just want to add a property to the input data you can take an even simpler approach and add the ping result with a calculated property:
Import-Csv $csvfile |
Select-Object Hostname,
@{n='Online';e={[bool](Test-Connection $_.Hostname -Count 1 -Quiet)}} |
Export-Csv 'C:\temp\pingresults.csv' -NoType
Appending to an array in a loop is not recommended, because appending essentially creates a new array with increased size, copies all elements from the old array, then adds the new elements to the new (empty) slots and releases the old array.
这篇关于如何通过管道将写入输出导出到导出 CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!